PHP文件上传报错UPLOAD_ERR_NO_TMP_DIR(错误码6)求助
Hey there, let's troubleshoot this upload error step by step. Since you haven't changed any configs and sys_get_temp_dir() correctly returns /tmp, the issue is almost certainly related to filesystem permissions, resource limits, or security restrictions—here's what to check:
1. Verify /tmp Directory Permissions & Ownership
PHP needs write access to the temp directory to store uploaded files temporarily.
- Log into your server and run this command to check
/tmp's status:
The output should show permissionsls -ld /tmpdrwxrwxrwt(thetis the sticky bit, which is critical for secure temp directory behavior) and owner/group asroot:root. - If permissions are incorrect, fix them with:
This sets the standard, secure permissions that allow all users to write tochmod 1777 /tmp/tmpwhile preventing unintended file deletion by non-owners.
2. Check if /tmp is Full or Out of Inodes
Even with correct permissions, a full filesystem or exhausted inodes will block new file creation:
- Check disk space usage for
/tmp:df -h /tmp - Check inode usage (each file/directory uses one inode):
df -i /tmp - If either is full, clean up old files in
/tmp. For example, delete files older than 7 days to free up space:find /tmp -type f -mtime +7 -delete
3. Check PHP's open_basedir Restriction
The open_basedir setting can limit which directories PHP can access, even if sys_get_temp_dir() returns /tmp:
- Open your
php.inifile (location varies by server—common paths are/etc/php/<version>/fpm/php.inior/etc/php/<version>/apache2/php.ini). - Look for the
open_basedirline. If it's set, make sure/tmpis included in the list of allowed directories (e.g.,open_basedir = /var/www/html/:/tmp/). - Save the file and restart your PHP service (e.g.,
systemctl restart php8.2-fpm) or web server to apply changes.
4. Check SELinux Policies (For Linux Servers With SELinux Enabled)
SELinux can block web server processes from writing to /tmp even with correct filesystem permissions:
- Temporarily disable SELinux to test if it's the cause:
setenforce 0 - Try uploading a file again. If it works, adjust SELinux policies instead of leaving it disabled:
- For Apache:
setsebool -P httpd_unified 1 - For Nginx:
setsebool -P nginx_unified 1
-Pflag makes the change persistent across server reboots. - For Apache:
5. Quick Check: File Size Limits (Just to Rule Out)
While your 91kb file is way below typical limits, it's worth confirming these php.ini settings are set appropriately:
upload_max_filesize = 2M(more than enough for your file)post_max_size = 8M(should be larger thanupload_max_filesize)- Restart PHP/web server after any changes.
After trying these steps, test your file upload again—this should resolve the UPLOAD_ERR_NO_TMP_DIR issue.
内容的提问来源于stack exchange,提问作者Mark

