PHP file upload fails because the temporary folder is missing or not writable
Missing a temporary folderShort 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.
Fix it fast
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']);2Match what you see, then jump to its fix
- 1
Error 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
Error 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
Error code 7, or move_uploaded_file() returns falseThe file is received, but the target folder cannot be writtenFix
- 1
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 -u2Fix
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-fpmConfirmed by php.net
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-fpmConfirmed by php.net
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/uploads2Fix
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/uploads3Verify
Upload again and confirm the file appears in the folder.
ls -l /var/www/site/uploads | tail -n 3Confirmed by php.net
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
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack 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 fixBack 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 fixBack end
CodeIgniter 3 stops with "Your system folder path does not appear to be set correctly"
503 Service Unavailable
CodeIgniter 3's front controller resolves $system_path and then checks that the result is a directory.
3 causes, each with a fix