Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP file upload fails because the temporary folder is missing or not writable

ErrorMissing a temporary folder

Short answer

PHP first saves every upload to a temporary directory. If that directory is missing, not writable by the PHP user, or blocked by open_basedir, the upload fails with error code 6 (UPLOAD_ERR_NO_TMP_DIR). Check upload_tmp_dir with php -i, make the folder writable, and allow it in open_basedir.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the upload error code and the values that decide where PHP stores uploads. The code says whether the problem is the temporary folder (6) or the final write (7).

    php -i | grep -E "upload_tmp_dir|open_basedir|file_uploads"
    # in your script: var_dump($_FILES['file']['error']);
  2. 2Match what you see, then jump to its fix

    1. 1Error code 6, and upload_tmp_dir shows a pathupload_tmp_dir is set to a folder that does not exist or cannot be writtenFix
    2. 2Error code 6 even though upload_tmp_dir is unset or correct, and open_basedir is not emptyopen_basedir does not allow the temporary directoryFix
    3. 3Error code 7, or move_uploaded_file() returns falseThe file is received, but the target folder cannot be writtenFix

What does the error look like?

  • Missing a temporary folder
  • $_FILES['file']['error'] is 6 (UPLOAD_ERR_NO_TMP_DIR)
  • $_FILES['file']['error'] is 7 (UPLOAD_ERR_CANT_WRITE), failed to write the file to disk

Cause 1 of 3

upload_tmp_dir is set to a folder that does not exist or cannot be written

1Check

The manual says upload_tmp_dir must be writable by whatever user PHP runs as. If it is not writable, PHP falls back to the system default temporary directory. Check the folder and who PHP runs as.

php -r 'echo ini_get("upload_tmp_dir"), PHP_EOL;'
ls -ld /var/tmp/php_uploads
ps -o user= -C php-fpm8.3 | sort -u

2Fix

Create the folder, give it to the PHP user, and point upload_tmp_dir at it. This setting is INI_SYSTEM, so it goes in php.ini or the pool file, not in .htaccess or ini_set().

sudo mkdir -p /var/tmp/php_uploads
sudo chown www-data:www-data /var/tmp/php_uploads
sudo chmod 750 /var/tmp/php_uploads
# php.ini
upload_tmp_dir = "/var/tmp/php_uploads"

3Verify

Restart PHP and upload a small file.

sudo systemctl restart php8.3-fpm

Confirmed by php.net

Did this fix it?

Cause 2 of 3

open_basedir does not allow the temporary directory

1Check

The manual says that with open_basedir on, the system default temporary directory must be allowed for an upload to succeed.

php -r 'echo ini_get("open_basedir"), PHP_EOL; echo sys_get_temp_dir(), PHP_EOL;'

2Fix

Add the temporary directory to open_basedir. The value is a list of directories separated by a colon on Linux.

# php.ini or pool file
open_basedir = "/var/www/site:/tmp"

3Verify

Restart PHP and repeat the upload.

sudo systemctl restart php8.3-fpm

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The file is received, but the target folder cannot be written

1Check

Code 7 means PHP could not write the file to disk. Check that the folder you move the upload to exists and that the PHP user can write to it.

ls -ld /var/www/site/uploads
df -h /var/www/site/uploads

2Fix

Give the PHP user write access to that folder, and free space if the disk is full.

sudo chown -R www-data:www-data /var/www/site/uploads
sudo chmod 755 /var/www/site/uploads

3Verify

Upload again and confirm the file appears in the folder.

ls -l /var/www/site/uploads | tail -n 3

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Check $_FILES['file']['error'] on every upload and show a clear message for codes 6 and 7.
  • Keep upload_tmp_dir on a folder only the PHP user can write, and out of the web root.
  • When you use open_basedir, list the temporary directory in it.
  • Watch disk space on the volume that holds uploads.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

CodeIgniter 4 cannot write its cache files

CodeIgniter\Cache\Exceptions\CacheException: Cache unable to write to /var/www/app/writable/cache/

CodeIgniter 4's file cache needs a cache directory that the web server can really write to.

2 causes, each with a fix

Back end

CodeIgniter 3 loads the model but the property is undefined

The model name you are loading is the name of a resource that is already being used: user

CodeIgniter 3 expects a model file named like its class, with the first letter capitalised, for example User_model.php holding class User_model.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.