PHP $_FILES array remains empty after file upload

ghz 7months ago ⋅ 90 views

I have spent hours trying solve this issue but nothing worked. I am trying to upload a csv file of size 0.5 kb.

This is my html form:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="Upload">
</form>

The $_FILES array remains empty after submit:

var_dump($_FILES); // array(0) { }

The $_POST array receives the name of the csv file:

var_dump($_POST); // array(1) { ["file"]=> string(11) "numbers.csv" }

From the php.ini file: (bottom two are commented out in the file)

file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20

;upload_tmp_dir =
;sys_temp_dir = "/tmp"

Perms on /var/tmp folder (if relevant)

drwxr-xr-x 8 root root 4096

Answers

If your $_FILES array is empty after submitting the form, it usually indicates that the file upload failed. There are several potential reasons for this issue. Here are some troubleshooting steps you can try:

  1. Check File Size Limit: Even though you've specified upload_max_filesize = 2M in your php.ini file, ensure that the size of your CSV file (numbers.csv) is within this limit. Since your file is only 0.5 KB, it should be well within the 2 MB limit.

  2. Check POST Max Size: Although you haven't mentioned it explicitly, ensure that the size of your entire POST request (including the file) does not exceed the post_max_size directive in your php.ini file. If it does, PHP might truncate the request, leading to an empty $_FILES array.

  3. Check File Upload Permissions: Ensure that the directory specified by the upload_tmp_dir directive (or the default temporary directory) has the correct permissions and is writable by the PHP process. Since you haven't specified upload_tmp_dir, PHP might be using the system's default temporary directory (/tmp or similar). Check the permissions of this directory and ensure that it's writable.

  4. Inspect PHP Error Log: Check your PHP error log for any messages related to file uploads. PHP might log errors or warnings that could provide more insight into why the upload failed.

  5. Test with a Smaller File: Try uploading a smaller file (e.g., a simple text file) to see if the issue persists. This can help determine if the problem is specific to the CSV file or if it's a more general issue with file uploads.

  6. Test on a Different Server: If possible, try running your code on a different server to see if the issue persists. This can help determine if the problem is related to server configuration or if it's specific to your current environment.

By systematically checking these factors, you should be able to identify and resolve the issue with your file upload. If you're still unable to upload files after trying these steps, further investigation may be necessary, such as checking server logs or consulting with your hosting provider.