PHP file upload error tmp_name is empty

PHP file upload error tmp_name is empty

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP file upload error tmp_name is empty

When debugging a file upload failure where $_FILES['document_attach']['tmp_name'] is empty and $_FILES['document_attach']['error'] returns 1, it is often tempting to assume the logic inside your PHP script is flawed. However, in this specific scenario, the issue almost invariably lies outside the application code: on the web server configuration or the client's operating system.

An error code of 1 in the $_FILES['error'] array specifically translates to UPLOAD_ERR_INI_SIZE. This indicates that the file exceeds the maximum size limit defined in your PHP configuration (upload_max_filesize). If you are checking the size against a custom variable like $maxSize = 5000000, you might overlook the server's hard ceiling. Even if your PDF is technically smaller than 5MB, the Apache or Nginx server (via php.ini) might have upload_max_filesize set to 2M or 1M by default. When a file hits this limit, PHP aborts the upload process before it can create the temporary file on disk. Consequently, tmp_name remains empty because there is no uploaded file to manage.

The reason one PDF uploads successfully while another fails is almost certainly due to file size variance. If the working file is 1.5MB and the failing one is 1.8MB, and your server limit is strictly 1.6MB, the latter will trigger UPLOAD_ERR_INI_SIZE. The validation logic in your script attempts to check the size after the upload has supposedly completed, but the failure occurs during the HTTP request parsing phase on the server side.

To resolve this without guessing limits, you must inspect the actual error message provided by PHP or check the configuration directly. You can dump the entire $_FILES array to see the specific error constant:

var_dump($_FILES['document_attach']);
// Expected output for error 1:
// 'error' => int(1) 

Once confirmed as an ini size issue, you have three primary paths to correction. The most robust solution is to increase the limit in php.ini. Locate the file (often at /etc/php/8.x/apache2/php.ini on Linux or via the control panel on shared hosting) and adjust both upload_max_filesize and post_max_size. It is critical that you set post_max_size slightly higher than your new upload_max_filesize; otherwise, PHP will reject larger files with an error code of 4 (UPLOAD_ERR_FORM_SIZE) because the POST data itself exceeds the allowed limit.

On shared hosting environments where you cannot edit php.ini, look for a .user.ini file in your project root or check if your host provides a "Max Upload Size" setting in the control panel (cPanel, Plesk, etc.). Alternatively, some hosts offer an .htaccess override:

php_value upload_max_filesize 10M
php_value post_max_size 10M

Note: This directive only works with Apache and mod_php. It will fail on Nginx or IIS unless the server is configured to pass these headers to PHP-FPM.

Finally, ensure that the tmp_dir (set in php.ini as upload_tmp_dir) exists and has write permissions for the web server user. If the temporary directory is full or inaccessible, uploads will fail silently or with random errors, though error 1 specifically points to size constraints. By aligning your client-side expectations with the server's hard limits, you can eliminate the inconsistency where some files upload while others do not.

Our network