0

I tried to write a basic upload php script and it doesn't work. Here's the HTML:

<html>
 <body>
  <form action="home.php" method="post" enctype="multipart/form-data">
   <input type="file" name="files_to_upload">
   <input type="hidden" name="MAX_FILE_SIZE" value="262144000">
   <input type="submit" value="upload">
  </form>
 </body>
</html>

And here's the PHP:

<?php
if (isset($_FILES['files_to_upload'])){
 if(@is_uploaded_file($_FILES['files_to_upload']['tmp_name'])) {
  echo "<br>";var_dump($_FILES);echo "<br>";
  if (move_uploaded_file($_FILES['files_to_upload']['tmpname'],'/home/user/pdf/'))
   echo '<script>alert("moved");</script>';
 }
}
?>

I don't get the moved alert , so the last function returns false I think. Here's a var_dump of $_FILES:

array(1) { ["files_to_upload"]=> array(5) { 
 ["name"]=> string(15) "O0903f21011.pdf 
 ["type"]=> string(15) "application/pdf"
 ["tmp_name"]=> string(14) "/tmp/phpgaSHzm" 
 ["error"]=> int(0) ["size"]=> int(314758) } 
} 

Edit: the directory /home/user/pdf has all rights granted to all users.

4
  • 2
    The destination needs to be a file name. You are using a directory. Try something like '/home/user/pdf/' . $_FILES['file_to_upload']['name'] Commented Feb 19, 2014 at 23:17
  • @Phil I just tried also with a filename, it's the same. I don't get the last alert. Commented Feb 19, 2014 at 23:18
  • 1
    For security reason, I would advice you to limit the File size on the server side, in your php and not in the form. any hacker can go through your input hidden form using firefox and can change it. <input type="hidden" name="MAX_FILE_SIZE" value="262144000000000"> Just as an advice incase you don't about this. Commented Feb 19, 2014 at 23:19
  • @Mubo To be honest, I've never seen a browser honour a MAX_FILE_SIZE hidden input :) Commented Feb 19, 2014 at 23:20

1 Answer 1

4

You have typo bug, replace:

 if (move_uploaded_file($_FILES['files_to_upload']['tmpname'],'/home/user/pdf/'))
   echo '<script>alert("moved");</script>';
 }

with

 if (move_uploaded_file($_FILES['files_to_upload']['tmp_name'],'/home/user/pdf/tmp_file.pdf'))
   echo '<script>alert("moved");</script>';
 }
Sign up to request clarification or add additional context in comments.

4 Comments

I edited as you suggested, but still can't get the last alert.
if there is still problem with moving file, you should see warning inside php.log. Also try using: echo 'Moved'; instead '<script>alert("moved")</script>';
This is the log: [Thu Feb 20 00:32:37.256211 2014] [:error] [pid 12582] [client 127.0.0.1:33025] PHP Warning: move_uploaded_file(): Unable to move '/tmp/php1JbDa9' to '/home/user/pdf/tmp_file.pdf'. I also tried to use echo "moved", but got nothing.
I solved. It was the parent directory with wrong permissions.

Your Answer

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