You drop an image into the WordPress media library and it fails with “Missing a temporary folder.” or “Failed to write file to disk.” next to the file name. Both messages come from PHP, which saves every upload in a temporary folder before WordPress sees it, and that first save failed. To fix it, find the folder PHP really uses and repair what broke it, which is usually a missing folder, a full disk or an account over its quota. The steps are written for site owners and admins with SSH access to a cPanel server or a VPS (virtual private server).
What you’ll accomplish
- Tell the two errors apart and know which part of the upload each one points to.
- Find the temporary folder PHP uses for web uploads, which is often not the one the command line reports.
- Check that folder’s permissions, free space, free inodes and the account quota.
- Give PHP a dedicated, writable temp folder that survives cPanel rebuilds.
- Prove the fix with a test upload that reports PHP’s own error code.
Prerequisites
You need SSH access to the server. The folder and quota checks work as the account user. Changing /tmp permissions or a PHP-FPM pool needs root. PHP-FPM is the service that keeps a set of PHP worker processes running for each site, and that set is called a pool.
The examples use a cPanel account named cpuser with its site in /home/cpuser/public_html, served as example.com with PHP 8.3. On a plain VPS, swap in your own site folder and PHP version. The steps apply to every current PHP release, because the two error codes have not changed in many years.
WP-CLI is handy but optional. It lets you query WordPress from the shell, and on cPanel it usually lives at /usr/local/bin/wp. Knowing which PHP handler runs the site helps in step 6. The handler is the way the web server starts PHP, such as PHP-FPM, CGI or suPHP. In cPanel, MultiPHP Manager shows it, and the probe in step 2 prints it too.
How it works
When a browser uploads a file, it sends a POST request to /wp-admin/async-upload.php. PHP reads that request before any WordPress code runs. It streams the file into a new file in its temporary folder, with a random name such as /tmp/php7Kq2Rx. Only after the whole request has arrived does WordPress get control. Its upload function, _wp_handle_upload(), then calls move_uploaded_file() to move the temp file into wp-content/uploads/.
PHP records the result of that first save as a number in $_FILES. WordPress turns the number into a sentence. Code 6, UPLOAD_ERR_NO_TMP_DIR, becomes “Missing a temporary folder.” Code 7, UPLOAD_ERR_CANT_WRITE, becomes “Failed to write file to disk.” The full list is on PHP’s file upload error codes page.
The difference between the two codes is the moment of failure.
Code 6 means PHP could not create the temp file at all. PHP first tries the folder set in upload_tmp_dir. If that setting is empty or the folder does not work, it falls back to the system temp folder. That is sys_temp_dir if set, then the TMPDIR environment variable, then /tmp. Code 6 appears only when the fallback fails too. It also appears when open_basedir (a PHP setting that fences scripts into a list of folders) does not include the fallback folder. PHP writes a warning to the error log each time.
Code 7 means PHP created the file but a write failed partway through. The file already exists, so permissions are no longer the problem. What runs out is space: the temp filesystem fills up, or the user’s disk quota is reached while the bytes arrive. PHP logs nothing for code 7 on a normal build, so this one is quieter.
This table sums up which cause produces which code.
| Cause | Code | Why |
|---|---|---|
| Temp folder missing, or not writable by the PHP user | 6 | The file cannot be created |
Fallback folder outside open_basedir | 6 | PHP refuses the folder before trying it |
| No free inodes, or the inode quota is reached | 6 | A new file needs an inode (the record a filesystem keeps for each file) |
| Filesystem mounted read-only | 6 | The file cannot be created |
| Temp filesystem out of space during the upload | 7 | The write stops partway |
| Account block quota reached during the upload | 7 | The write stops partway |
A noexec mount on /tmp causes neither error. noexec only stops files in that folder from being run as programs, and PHP never runs an uploaded file. If uploads succeed but image or PDF tools break afterwards, see PHP-FPM uploads failing because /tmp is mounted noexec.
One more setting causes confusion. WP_TEMP_DIR in wp-config.php changes the folder WordPress’s own get_temp_dir() returns. WordPress uses that folder for plugin, theme and core update downloads, and for importing files from a URL. PHP has already saved the browser’s upload before WordPress starts, so WP_TEMP_DIR has no effect on these two errors. The fix lives in PHP’s settings.
Step-by-step
1. Confirm the message and look for PHP’s warning. Note which of the two sentences WordPress shows. Then search the PHP error log for the warning that goes with code 6. On cPanel with PHP-FPM, the log is usually in /home/cpuser/logs/. With other handlers it is often an error_log file in the site folder.
grep -rh "unable to create a temporary file" /home/cpuser/logs/ /home/cpuser/public_html/error_log 2>/dev/null | tail -3
Example output:
[30-Sep-2026 09:41:57 UTC] PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0
This line confirms code 6. No output with a code 6 message means the log lives elsewhere, which does not change the next steps. For code 7, expect no log line and move on.
2. Find the temp folder PHP uses for web requests. The command line often uses a different php.ini from the web server, so ask the web process itself. Create a small probe file with a name nobody will guess.
cat > /home/cpuser/public_html/tmpcheck-7f3a.php <<'PHP'
<?php
header('Content-Type: text/plain');
echo 'SAPI: ', php_sapi_name(), "\n";
echo 'upload_tmp_dir: ', var_export(ini_get('upload_tmp_dir'), true), "\n";
echo 'sys_get_temp_dir: ', sys_get_temp_dir(), "\n";
echo 'open_basedir: ', var_export(ini_get('open_basedir'), true), "\n";
echo 'running as: ', function_exists('posix_geteuid') ? posix_getpwuid(posix_geteuid())['name'] : 'unknown', "\n";
foreach ($_FILES as $f) { echo 'upload error code: ', $f['error'], "\n"; }
PHP
chown cpuser:cpuser /home/cpuser/public_html/tmpcheck-7f3a.php
curl -s -F "f=@/etc/hostname" https://example.com/tmpcheck-7f3a.php
Example output:
SAPI: fpm-fcgi
upload_tmp_dir: ''
sys_get_temp_dir: /tmp
open_basedir: ''
running as: cpuser
upload error code: 7
The folder PHP uses is upload_tmp_dir when it is set and usable, otherwise sys_get_temp_dir. Here it is /tmp. The SAPI line names the handler, for example fpm-fcgi for PHP-FPM or cgi-fcgi for CGI. The last line reproduces the error outside WordPress, so you can retest in seconds. If open_basedir is not empty and upload_tmp_dir is empty, the system temp folder must sit inside one of the paths open_basedir lists.
wp eval 'echo sys_get_temp_dir(), PHP_EOL, ini_get("upload_tmp_dir"), PHP_EOL;' prints the same values for the command-line PHP. Treat it as a hint only. When it disagrees with the probe, the probe is right.
3. Check the folder exists and the PHP user can write to it. Use the folder from step 2.
ls -ld /tmp /home/cpuser/tmp
findmnt -T /tmp
Example output:
drwxrwxrwt 14 root root 4096 Sep 30 08:12 /tmp
drwx------ 2 cpuser cpuser 4096 Sep 12 17:30 /home/cpuser/tmp
TARGET SOURCE FSTYPE OPTIONS
/tmp /dev/loop0 ext4 rw,nosuid,noexec,relatime
A shared /tmp must show drwxrwxrwt owned by root, which is mode 1777. The final t is the sticky bit, which stops users from deleting each other’s files. Some hardened servers use 1733 instead, which still lets PHP create files. A per-account folder should belong to the user PHP runs as, shown on the probe’s running as line, with mode 700 or 750. An ro in the OPTIONS column means read-only, which also gives code 6.
If the permissions are wrong or the folder is missing, fix them as root.
chown root:root /tmp && chmod 1777 /tmp
mkdir -p /home/cpuser/tmp && chown cpuser:cpuser /home/cpuser/tmp && chmod 700 /home/cpuser/tmp
Neither command prints anything on success. Run ls -ld again and compare with the healthy lines above.
4. Check free space and free inodes on that folder. Run both, because each code maps to a different column.
df -h /tmp
df -i /tmp
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 4.0G 4.0G 0 100% /tmp
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/loop0 262144 18233 243911 7% /tmp
Use% at or near 100% explains code 7. IFree at 0 explains code 6. The temp folder also needs free space at least as large as your biggest upload, times the number of uploads running at once. On cPanel, /tmp is often a small separate filesystem created by cPanel’s securetmp script, so it fills long before the main disk.
To see what is taking the room, list the biggest items.
du -xh --max-depth=1 /tmp 2>/dev/null | sort -h | tail -5
PHP deletes its own php* temp files when each request ends. Leftovers usually come from crashed processes, backups or plugins. Remove files you recognise as stale, and never delete sess_* files, which are live PHP sessions.
5. Check the account quota. If the temp folder is inside the account’s home, or /tmp sits on a filesystem with quotas, the upload counts against the user’s quota.
quota -s cpuser
Example output:
Disk quotas for user cpuser (uid 1005):
Filesystem space quota limit grace files quota limit grace
/dev/vda1 10240M* 10240M 10240M 311k 0 0
An asterisk after a number means the user is at or over that limit. Over on space gives code 7, over on files gives code 6. Raise the quota in WHM under Modify an Account, or free space in the account. In the cPanel interface, the Disk Usage figure in the sidebar shows the same limit.
6. Give PHP a dedicated temp folder. A folder inside the account’s home keeps one site’s uploads from filling a shared /tmp. Create it as in step 3, then set upload_tmp_dir. PHP accepts this setting only at the system or pool level. It is ignored in .user.ini and .htaccess, so pick the method that matches your handler.
On cPanel with PHP-FPM, edit the domain’s pool settings file, /var/cpanel/userdata/cpuser/example.com.php-fpm.yaml, as root. cPanel builds the real pool file from it, so the change survives rebuilds. If the file does not exist, create it with both lines below. If it exists, it already starts with ---, so add only the second line at the end. The name and value format is the one cPanel’s “Configuration Values of PHP-FPM” page uses for pool settings. A typo in this file can stop PHP-FPM from starting, so copy the line exactly.
---
php_admin_value_upload_tmp_dir: { name: 'php_admin_value[upload_tmp_dir]', value: /home/cpuser/tmp }
Then rebuild the pool files, restart PHP-FPM and check the generated pool file.
/scripts/php_fpm_config --rebuild
/scripts/restartsrv_apache_php_fpm
grep upload_tmp_dir /opt/cpanel/ea-php83/root/etc/php-fpm.d/example.com.conf
The grep should print php_admin_value[upload_tmp_dir] = /home/cpuser/tmp. Use the ea-php number that matches the site’s PHP version, such as ea-php82 for PHP 8.2. If the grep prints nothing, cPanel did not pick up the setting. Check the file for a typo and run the rebuild again before you go further.
On a VPS with PHP-FPM, create the folder for the pool’s user, add the setting to the pool file, then test the config and reload. The example uses the default www pool on Debian or Ubuntu, which runs as www-data. If the site has its own pool, use that pool’s file and its user value instead.
mkdir -p /var/www/example.com/tmp && chown www-data:www-data /var/www/example.com/tmp && chmod 700 /var/www/example.com/tmp
echo 'php_admin_value[upload_tmp_dir] = /var/www/example.com/tmp' >> /etc/php/8.3/fpm/pool.d/www.conf
php-fpm8.3 -t && systemctl reload php8.3-fpm
A good test prints a line ending in test is successful.
With the CGI or suPHP handler on cPanel, open MultiPHP INI Editor in cPanel, choose Editor Mode, pick the domain and add upload_tmp_dir = /home/cpuser/tmp. Those handlers start a fresh PHP process that reads a php.ini from the site folder. PHP-FPM ignores that file, which is why it needs the pool setting instead.
7. Rerun the probe. The same curl command from step 2 should now show your new folder and a clean upload.
curl -s -F "f=@/etc/hostname" https://example.com/tmpcheck-7f3a.php
Look for upload_tmp_dir: '/home/cpuser/tmp' and upload error code: 0. If the old value still shows, the pool was not reloaded or you edited the wrong file.
Validate it works
First, confirm the probe reports upload error code: 0 for a file close to your real upload size. Create a 20 MB test file and send it.
head -c 20M /dev/urandom > ~/upload-test.bin
curl -s -F "f=@$HOME/upload-test.bin" https://example.com/tmpcheck-7f3a.php | tail -1
Expected output is upload error code: 0. A 1 means the file is over upload_max_filesize, which is a size limit, not a temp folder problem. Use a smaller file, or raise the limit. If the last line is running as: cpuser with no error code at all, the request went over post_max_size and PHP dropped the whole upload. Use a smaller file for this test.
Next, upload a real image through Media → Add Media File in WordPress (Add New Media File on older versions). It should appear in the library with its thumbnail. Confirm from the command line that WordPress recorded it.
wp post list --post_type=attachment --posts_per_page=1 --fields=ID,post_title,post_date --path=/home/cpuser/public_html
The newest row should be the file you uploaded, with today’s date. Note that wp media import does not prove the fix, because it copies files without going through PHP’s upload handling.
Finally, delete the probe and the test file.
rm /home/cpuser/public_html/tmpcheck-7f3a.php ~/upload-test.bin
Troubleshooting
The probe still shows the old upload_tmp_dir. The site runs under a different handler or PHP version than the one you edited. Check the SAPI line and MultiPHP Manager, then edit the matching pool or php.ini.
The log says “file created in the system’s temporary directory”. This notice means your configured upload_tmp_dir did not work and PHP fell back to the system folder. PHP writes this notice before it tries the fallback, so the upload may still work or may still fail with code 6. Either way, check that the configured folder exists and that the PHP user owns it, as in step 3.
Code 7 only happens with large files. The temp filesystem has some free space, but not enough for big uploads, especially several at once. Move upload_tmp_dir to a folder in the account’s home, as in step 6. The main disk usually has more room, but uploads there count against the account quota, so check step 5 again.
The error changes to “The uploaded file could not be moved to wp-content/uploads/…”. The temp folder now works, and the failure moved to the next stage. The uploads folder is not writable by the PHP user. The media upload HTTP error guide covers ownership of wp-content/uploads.
Uploads work, but plugin updates fail with “Download failed. Destination directory for file streaming does not exist or is not writable.” That message is the WP_TEMP_DIR case. Define WP_TEMP_DIR in wp-config.php to point at a folder the account owns, such as /home/cpuser/tmp.
When this is the wrong approach
On shared hosting without root access, you cannot change the pool or clean /tmp. Send the probe output to your host’s support desk. It shows them the folder, the user and the error code in one message, which saves a round of questions.
If df -h / shows the whole server disk is full, clearing /tmp only buys time. Find what is filling the main disk before other services start failing too.
If the upload fails with a different message, such as a bare “HTTP error”, these steps will not find it. The two sentences above only ever come from PHP error codes 6 and 7. Any other message starts earlier or later in the upload path.
Running WordPress that earns real money deserves an operator — Stack Harbor runs that stack under Enterprise WordPress; book an infrastructure consult.