Why would $_FILES be empty when uploading files to PHP?

前端 未结 21 2713
一向
一向 2020-11-21 22:37

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 PH

相关标签:
21条回答
  • 2020-11-21 23:21

    Another possible culprit is apache redirects. In my case I had apache's httpd.conf set up to redirect certain pages on our site to http versions, and other pages to https versions of the page, if they weren't already. The page on which I had a form with a file input was one of the pages configured to force ssl, but the page designated as the action of the form was configured to be http. So the page would submit the upload to the ssl version of the action page, but apache was redirecting it to the http version of the page and the post data, including the uploaded file was lost.

    0 讨论(0)
  • 2020-11-21 23:22

    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>
    
    0 讨论(0)
  • 2020-11-21 23:24

    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>
    
    0 讨论(0)
  • 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>
    
    0 讨论(0)
  • 2020-11-21 23:26

    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

    0 讨论(0)
  • 2020-11-21 23:28

    Do not trust the temp folder location provided by sys_get_temp_dir if you're in a shared hosting environment.

    Here is one more thing to check that has not yet been mentioned...

    I assumed, naturally, that the folder where my PHP script stored temporary file uploads was /tmp. This belief was reinforced by the fact that echo sys_get_temp_dir() . PHP_EOL; returns/tmp. Also, echo ini_get('upload_tmp_dir'); returns nothing.

    To verify that the uploaded file does in fact briefly appear in my /tmp folder, I added a sleep(30); statement to my script (as suggested here) and navigated to my /tmp folder in cPanel File Manager to locate the file. However, no matter what, the uploaded file was nowhere to be found there.

    I spent hours trying to determine the reason for this, and implemented every suggestion that's been offered here.

    Finally, after searching my website files for the query tmp, I discovered that my site contained other folders named tmp in different directories. I realized that my PHP script was actually writing the uploaded files to .cagefs/tmp. (The "Show Hidden Files" setting must be enabled in cPanel in order to view this folder.)

    So, why does the sys_get_temp_dir function return inaccurate info?

    Here's an explanation from the PHP.net webpage for sys_get_temp_dir (i.e., the top comment):

    If running on a Linux system where systemd has PrivateTmp=true (which is the default on CentOS 7 and perhaps other newer distros), this function will simply return "/tmp", not the true, much longer, somewhat dynamic path.

    This SO post delves into the issue, as well:

    • Stack Overflow: sys_get_temp_dir in shared hosting environment
    0 讨论(0)
提交回复
热议问题