954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP File Upload Help

i want to create a successful file upload component in a site and i have used and gone thru countless tutorials but it does not seem to work.

here is the code i am using

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />

<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

and the php script is

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "";
  echo "Type: " . $_FILES["file"]["type"] . "";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?> 

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "";
    echo "Type: " . $_FILES["file"]["type"] . "";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?> 
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "";
    echo "Type: " . $_FILES["file"]["type"] . "";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

these codes weer taken from w3schools.com

any idea why i cannot upload files?

rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

file permission may be the issue on your server.

urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in D:\xampp\htdocs\drupal\upload.php on line 12

it keeps giving errors like that.

can u tell me how i can fix the file permissions???

rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Something you missed over there relevant to quotes, doublequotes or semicolor or some other!, Again copy and paste your code It will work. Your given code itself working fine and I tested it.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

does the file upload happen for u??
u mean copy paste what i entered here is it? it is the same thing that doesnt work right?

rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Yes, I created a folder named 'upload' where my php and html file resides. Then executed the html file from localhost and upload a gif file while submit the gif is stored in the relevant upload folder promptly. So nothing I found an error. I think have to test with real server. But Is it work for you from localhost?

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

i am currently using drupal - but still some error comes on that
so i tested it separately by savung the files in htdocs
this is what i get when i uploaded a jpeg file

Upload: DSC00792.JPG
Type: image/jpeg
Size: 124.9560546875 Kb
Stored in: D:\xampp\tmp\php806A.tmpInvalid fileInvalid file

whats wrong?

rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

it seems to get saved as a .tmp file in the tmp folder..... why isnt it saving as a jpeg???

rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

For me also its came, .tmp file I think It was an intermediate file which has to be processed and I checked in the tmp folder of xampp nothing with the name php806A.tmp. But the file is created. do one thing, send me the same jpg file here, i will test with that jpeg.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

i uploaded.
please check

Attachments DSC00793.JPG 127.51KB
rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

ok i used a diff code and file gets uploaded fine

http://www.quackit.com/php/tutorial/php_upload_file.cfm

check that

do u know anytng abt ezpdf?

rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Yes, that jpeg is not uploaded shows the msg as yours. Sorry I dont know about ezpdf.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

Based on the previous replies here, I conclude that there is still no solution. The following code will upload any file (test for file-type is not included yet) to the folder "upload".

<?php
// set the destination
$uploaddir="upload/";

// test for errors
if ($_FILES['file']['error'] > 0) {
	echo "There is a problem: ";
	switch ($_FILES['file']['error']) {
		case 1:  echo "File greater as allowed in html file"; break;
		case 2:  echo "File greater as allowed in php.ini"; break;
		case 3:  echo "File only partial recieved"; break;
		case 4:  echo "No file recieved"; break;
	}
	exit;
}

// you could test here if you get the correct image
// $mime = $_FILES['file']['type'];
// 1=gif, 2=jpeg, 3=png

// set full path + filename
$upfile = $uploaddir . $_FILES['file']['name'];

// check the actual upload
if (is_uploaded_file($_FILES['file']['tmp_name'])) {
	if (!move_uploaded_file($_FILES['file']['tmp_name'], $upfile)) {
		echo "Unable to move file to destination.";
		exit;
	}
}
else {
	echo "There was a problem uploading your file.";
	exit;
}

// file is uploaded in $uploaddir
echo "File upload succesvol";
?>
colweb
Posting Whiz
318 posts since Nov 2007
Reputation Points: 34
Solved Threads: 52
 

ok thank you.. but how can i create a more user specific upload? where the users who upload will be identified with their submission?

rukshilag
Junior Poster
101 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
ok thank you.. but how can i create a more user specific upload? where the users who upload will be identified with their submission?

Didn't see that in the original question. And the html code containing the form doesn't contain anything about that. But anyway, suppose a user did login and want to upload a file. Then you probably have an user_id (from a mysql database).

Change part of the code I posted to this:

<?php
// set the destination
$uploaddir="upload/".$user_id;

For example user somebody with user_id 5298 upload file "demojpg.jpg". It will be uploaded to the folder "upload" with the name "5298demojpg.jpg".
You could also make individual folders for every user and upload it to that folder. If you want that, change the code to:

<?php
// set the destination
$uploaddir="upload/".$user_id."/";


And don't forget, (like I did) to use $user_id = $_POST['user_id']; otherwise you don't have a user_id variable in the upload php file.

colweb
Posting Whiz
318 posts since Nov 2007
Reputation Points: 34
Solved Threads: 52
 

try this:

if (isset($_FILES["file"])) {
   @list(, ,$imtype, ) = getimagesize($_FILES['file']['tmp_name']);
   if ($imtype != 3) {
      if ($imtype != 2) {
         if (imtype != 1) {
            echo "Invalid format";
         }
      }
   }
   if($_FILES['file']['error'] != UPLOAD_ERR_OK) {
      echo "Upload file error";
   }
   else {
      if(!is_uploaded_file($_FILES['file']['tmp_name'])) {
          echo "Invalid request";
      }
      else {
          $name = $_FILES['file']['name'];
          $upload_path = "/upload/$name";
          if (file_exists("upload/".$name)) echo "$name already exists.";
          else move_uploaded_file($_FILES['file']['tmp_name'], $upload_path);
      }
   }     
}
Liyaan
Newbie Poster
4 posts since May 2010
Reputation Points: 10
Solved Threads: 1
 

anyone can access filename of input=file using $_POST instead of $_FILES?

ricmetal
Newbie Poster
1 post since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: