Why would $_FILES be empty when uploading files to

2018-12-31 03:56发布

I have WampServer 2 installed on my Windows 7 computer. I'm using Apache 2.2.11 and PHP 5.2.11. When I attempt to upload any file from a form, it seems to upload, but in PHP, the $_FILES array is empty. There is no file in the c:\wamp\tmp folder. I have configured php.ini to allow file uploads and such. The tmp folder has read/write privileges for the current user. I'm stumped.

HTML:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <form enctype="multipart/form-data" action="vanilla-upload.php" method="POST">
        Choose a file to upload: <input name="uploadedfile" type="file" /><br />
        <input type="submit" value="Upload File" />
    </form>
</body>
</html>

PHP:

<?php
echo 'file count=', count($_FILES),"\n";
var_dump($_FILES);
echo "\n";
?>

20条回答
路过你的时光
2楼-- · 2018-12-31 04:29

If you are using JQuery Mobile

Using a multipart form with a file input is not supported by Ajax. In this case you should decorate the parent form with data-ajax="false" to ensure the form is submitted properly to the server.

<form action="upload.php" method="post" enctype="multipart/form-data"  data-ajax="false">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
查看更多
永恒的永恒
3楼-- · 2018-12-31 04:30

I was struggling with the same problem and testing everything, not getting error reporting and nothing seemed to be wrong. I had error_reporting(E_ALL) But suddenly I realized that I had not checked the apache log and voilà! There was a syntax error on the script...! (a missing "}" )

So, even though this is something evident to be checked, it can be forgotten... In my case (linux) it is at:

/var/log/apache2/error.log
查看更多
临风纵饮
4楼-- · 2018-12-31 04:32

It is important to add enctype="multipart/form-data" to your form, example

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
查看更多
十年一品温如言
5楼-- · 2018-12-31 04:32

I was empty $_FILES because after <form enctype="multipart/form-data" method="post"> i placed

</div>
<div style="clear:both"></div>

Initial code was like

<span class="span_left">Photos (gif/jpg/jpeg/png) </span>
<form enctype="multipart/form-data" method="post">
<input name="files[]" type="file" id="upload_file" />
<input type="button" id="upload" value="Upload photo" />
</form>

I decided to modify and

<div>
<span class="span_left">Photos (gif/jpg/jpeg/png) </span>
<form enctype="multipart/form-data" method="post">
</div>
<div style="clear:both"></div>
<input name="files[]" type="file" id="upload_file" />
<input type="button" id="upload" value="Upload photo" />
</form>
<div style="clear:both"></div>

So conclusion is that after <form enctype="multipart/form-data" method="post"> must be <input name, type, id and must not be <div> or some other tags

In my situation correct code was

<div>
<span class="span_left">Photos (gif/jpg/jpeg/png) </span>
</div>
<div style="clear:both"></div>
<form enctype="multipart/form-data" method="post">
<input name="files[]" type="file" id="upload_file" />
<input type="button" id="upload" value="Upload photo" />
</form>
<div style="clear:both"></div>
查看更多
伤终究还是伤i
6楼-- · 2018-12-31 04:34

Check your php.ini for enable_post_data_reading=On , because:

Disabling this option causes $_POST and $_FILES not to be populated. The only way to read postdata will then be through the php://input stream wrapper. (... )

In http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading

查看更多
人气声优
7楼-- · 2018-12-31 04:34

If your main script is http://Some_long_URL/index.php be carefull to specify the full URL (with explicit index.php and not only http://Some_long_URL) in the action field. Surprisingly, if not, the right script is executed, but with en empty $_FILES !

查看更多
登录 后发表回答