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

前端 未结 21 3268
一向
一向 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: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

提交回复
热议问题