Setting PHP tmp dir - PHP upload not working
When building a multi-part file uploader like Valums File Uploader, you often encounter a specific bottleneck related to memory management during the transfer. The workflow typically involves the client browser breaking a large file into chunks and sending them sequentially to the server. The server accepts these chunks temporarily before reassembling them into the final file at the destination path.
In many legacy hosting environments or containerized setups, the system default temporary directory (/tmp) is mounted as a RAM disk with strict size limitsâsuch as 4 MB in your specific case. This configuration works perfectly for small assets but causes immediate failures for anything exceeding that threshold. You might set upload_tmp_dir in your php.ini to point to a writable /upload directory, verify permissions are correct, and confirm the web server user owns the files. Yet, uploads larger than the RAM disk limit still fail.
This discrepancy often stems from a misunderstanding of how PHP prioritizes its temporary storage locations. When you call sys_get_temp_dir(), it returns the system default, but that does not mean PHP is ignoring your configuration. The issue lies in the precedence order of environment variables versus configuration files.
PHP checks for the TMP or TEMP environment variables first. If these are set (common in Linux distributions like Debian or Ubuntu), they override the upload_tmp_dir directive found in php.ini. Even if you have configured upload_tmp_dir = /upload, a system-wide environment variable pointing to /tmp will take precedence for single-part uploads and potentially for multipart chunk storage depending on the SAPI.
To resolve this without relying solely on global php.ini changes, you must explicitly clear the environment variables before PHP initializes its configuration parser. You can do this by adding the following lines to your .htaccess file or placing them in a script that runs before any logic requiring uploads:
php_value upload_tmp_dir /upload
php_value tmp_dir /upload
php_value temp_dir /upload
However, modifying php.ini globally is often insufficient if you are running multiple applications on the same server. A more robust approach involves using the putenv() function in your application entry point to unset these variables before including any configuration:
// In index.php or app bootstrap file
if (getenv('TMP')) {
putenv('TMP');
}
if (getenv('TEMP')) {
putenv('TEMP');
}
if (getenv('TMPDIR')) {
putenv('TMPDIR');
}
Once these environment variables are unset, PHP falls back to reading the upload_tmp_dir directive from php.ini or your local configuration. Ensure that the /upload directory exists and is writable by both the web server process (often www-data) and any background processes handling file moves.
It is also worth noting that sys_get_temp_dir() will continue to return /tmp even after you successfully redirect uploads to /upload. This function reflects the system default, not the active upload directory for multipart requests. To verify your fix works dynamically without relying on static output, inspect the actual uploaded file location within your application logic immediately after a chunk is received or a full upload completes. If the file appears in /upload, the configuration is successful.
If you are using Laravel, ensure you have not overridden these settings in config/filesystems.php or if you are running Forge deployments where environment variables might be injected automatically. For complex enterprise setups, always validate that open_basedir restrictions do not inadvertently block access to your custom directory.
