5

I just made a file upload code and I was wondering why the file upload wasn't working. I changed the upload dir to 0777. This is my upload HTML code:

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

And PHP Code:

<?php
if ($_FILES["file"]["error"] > 0){
echo "Error Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Uploaded file: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kilobytes<br />";

if (file_exists("/files/".$_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. No joke-- this error is almost <i><b>impossible</b></i> to get. Try again, I bet 1 million dollars it won't ever happen again.";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],"/filebro/".$_FILES["file"]["name"]);
  echo "Done";
  }
}
?>

So, what could the problem possibly be?

4
  • Please define "not working". Do you get any error messages? Do you have error reporting on?
    – deceze
    Mar 31, 2012 at 1:51
  • Do you mean to be using your paths as actually root paths on the server, or are those supposed to be relative to your web root?
    – jprofitt
    Mar 31, 2012 at 1:51
  • you should be aware that someone could upload .htaccess files and do lots of fun things with this Mar 31, 2012 at 1:54
  • I do have error reporting on, but I get nothing. No errors. Mar 31, 2012 at 1:55

2 Answers 2

11

Try

move_uploaded_file($_FILES["file"]["tmp_name"],"filebro/".$_FILES["file"]["name"]);
0
0

Your

if (file_exists("/files/".$_FILES["file"]["name"]))

will always return false. The file is saved with its temp name.

Try

if (is_uploaded_file($_FILES["file"]["tmp_name"]))

instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.