LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2011, 08:12 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Php question about checking if a input="file" file exists


Hey I have had a problem about a php file called register.php that when the web browser directly asked to open register.php an error would display because no username was sent to the php file register.php. I fixed my syntax checking by using the code
PHP Code:
if (!isset($_POST["username"]) || !$_POST["username"]) 
Now I am having a similar problem while trying to create upload_file.php that uploads files but if someone accesses upload_file.php directly my new php server gives me a butt load of errors that insist that $_FILE['file'] is not defined. So I was wondering if any code show me how to properly check to see if $_FILE['file'] exists so that if it doesnt I could just print a message saying to make sure to enter a file and then exit the php script.

Below is the current php file that works but will print errors if accessed directly with a web browser. Also below that I am posting the html page so you guys can test if.

upload_file.php
PHP Code:
<?php
if($_FILES["file"]["error"] > 0)
{
    echo 
"Error: " $_FILES["file"]["error"] . "<br>";
}
else
{
    if(
$_FILES["file"]["size"] > 570000)
    {
        echo 
"File size to exceeds 5.7 Megabytes";
        exit;
    }
    if(
file_exists("uploads/" $_FILES["file"]["name"]))
    {
        echo 
$_FILES["file"]["name"] . " already exists. ";
        exit;
    }
    else
    {
        
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" $_FILES['file']['name']);
        echo 
"Upload   : " . ($_FILES["file"]["name"]) . "<br>";
        echo 
"Type     : " . ($_FILES["file"]["type"]) . "<br>";
        echo 
"Size     : " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
        echo 
"TMP File : " $_FILES["file"]["tmp_name"] . "<br>";
        echo 
"Stored In: " "uploads/" $_FILES["file"]["name"];
  }
}
?>
upload_file.html
Code:
<html>
<head>
<title>Blank HTML</title>
<meta name="author" content="Eric Justin Allan">
<style type="text/css">
body 
{
	background-image: url('jpg/200829053409-8159.jpg');
	color:blue;
}
a 
{
	text-decoration:none;
	color:white;
}
</style>
</head>





<body>
	<p>Unpload files smaller then 5.7 megabytes preferably small files but definetly cool files
	<form action="upload_file.php" method="post" enctype="multipart/form-data">
	<font color="red">*</font>&nbsp; Filename:<input type="file" name="file" id="file" />
	<input type="submit" name="submit" value="Submit" /></form>
</body>
</html>
 
Old 12-11-2011, 04:32 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You could add a constant in every page where it is ok to access directly and test if this constant is defined in the pages that you don't want to be directly accessed, like:

In top of directly accessed pages:
PHP Code:
define("ACCESS"1); 
And in top of other pages:
PHP Code:
if (!defined('ACCESS')) {
    
/* this is an example, you could also redirect to another page, 
       like index.php... */
    
header("HTTP/1.1 403 Forbidden");
    exit(
1);

 
Old 12-11-2011, 08:03 AM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
It doesnt seem to be working
 
Old 12-11-2011, 08:37 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You put the 1st code in register.php and the 2nd in upload_file.php ?
Near the top, right after the <?php tag ?
 
Old 12-11-2011, 05:58 PM   #5
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Thankyou
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File Exists but... "-bash: No such file or directory" zachet Linux - Newbie 24 03-20-2016 01:13 AM
[SOLVED] "No such file or directory" when file exists Snark1994 Linux - Software 4 05-22-2011 10:20 AM
[SOLVED] "start: Unknown job" but file exists mahmoodn Ubuntu 4 05-12-2011 10:00 AM
Checking if "/grub/stage1" exists... no centguy Linux - Software 4 06-15-2009 08:50 AM
kget: "file already exists" ungua Linux - Software 2 02-17-2007 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration