简体   繁体   中英

PHP file upload doesn't work

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM