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" />
<br />
<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"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  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"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    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"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    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?

Recommended Answers

All 16 Replies

file permission may be the issue on your server.

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???

Member Avatar for rajarajan2017

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.

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?

Member Avatar for rajarajan2017

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?

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?

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

Member Avatar for rajarajan2017

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.

i uploaded.
please check

Member Avatar for rajarajan2017

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

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";
?>

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

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; otherwise you don't have a user_id variable in the upload php file.

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);
      }
   }     
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.