PHP - $_FILES array is empty [duplicate]

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

This question already has an answer here:

Yes, the enctype attribute is set. Other forms/form-hanlders work fine so the temp directory must be writable. I'm out of Ideas.

I checked the post values and $_POST['file'] exists and contains the name of the file.

Here is my form and the PHP that handles it. What am I missing?

<form action='orl_ftp.php' method='post' enctype='multipart/form-data'>     <table>         <tr>             <td>Choose File: </td>             <td><INPUT type='file' id='file' name='file'></td>         </tr>         <tr>             <td>&nbsp;</td>             <td><INPUT type='submit' name='Submit' value='Process'></td>         </tr>     </table> </form> 

And the relevant PHP code. Note that the $_FILES array is set, it's just empty.

if(isset($_POST['Submit'])){     $upload_results = "";     if(!isset($_FILES)){$upload_results .= "No files uploaded"; }     if($upload_results == ""){          echo "<pre>";         var_dump($_FILES);         exit;          // ...      } } 

回答1:

On line 101:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" />                                                                  ^ 

This is causing the issue, the browser it keeping this form open and therefore the missing enctype is the issue. Remove or close this form properly.

Example:

<?php if(isset($_POST['Submit'])){     $upload_results = "";     if(!isset($_FILES)){$upload_results .= "No files uploaded"; }     if($upload_results == ""){         echo "<pre>";         var_dump($_FILES);         exit;     } } ?> <form action='' method="post" /> <form action='' method='post' enctype='multipart/form-data'>     <table>         <tr>             <td>Choose File: </td>             <td><INPUT type='file' id='file' name='file'></td>         </tr>         <tr>             <td>&nbsp;</td>             <td><INPUT type='submit' name='Submit' value='Process'></td>         </tr>     </table> </form> 

This will not post any files.



回答2:

You have multiple forms in the same script and so each of them needs the enctype='multipart/form-data'

Also, it doesn't look like you close the first form and doing <form ... /> is not valid html.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!