Changing PHP Temp Directory

2019-05-29 08:02发布

问题:

I have a site for media conversion where users can upload video, audio or image files and it is converted in to 3 popular formats. Conversion script works fine but I am having some issues with the tmp directory where the files get uploaded to. I have tried 2 scenarios I am fine with either but neither works and it seems to me to be permissions related but I can seem to fix the problem. I am working locally right now while I work out the kinks.

Scenario 1:

  • File is uploaded to default local tmp directory (C:\WINDOWS\tmp) - works fine
  • Attempt to run conversion script using tmp file that was uploaded and it doesn't work - run from command line works perfectly fine though

Scenario 2:

  • File is uploaded to directory I have given IIS_IUSRS full control of (for testing) and file won't upload - yes the directory is spelt correctly (I changed the upload_tmp_dir value in php.ini)

Both the site the javascript that send the XMLHttpRequest to the PHP file, as well as the site the PHP file itself reside on are IIS sites so I assume the script is being run as IIS_IUSRS.

EDIT: Temp file is no longer being created at all for Scenario 1, can't figure out why I am assuming playing with permission messed something up because the code hasn't changed. I've given Modify to IIS_USRS and USERS to try and get it working again but no luck :( although the error log is still writing to the same folder...weird

NOTE: The "tmp_name" value of the $_FILES variable I am sending still has a value of "C:\WINDOWS\Temp\'filename'" but the file is not there

EDIT: Another new development, it appears it is NOT a permissions issue because I can create a temp file via $temp_file = tempnam(sys_get_temp_dir(), 'Test'); however it obviously does not contain the uploaded data so it does not solve my problem

回答1:

This is less of a problem solved and more of a workaround. The issue seems to be something with the $_FILES['file']['tmp_name'];

When I echo the contents it looks as I expect however no file appears. So rather than taking advantage of that process that happens naturally, I have had to take a manual approach. What I have done is the following:

create a temp file

$temp_file = tempnam(ini_get('upload_tmp_dir'), 'php');

then add the content from the temp file created during the $_POST request. (which some how are in the $_FILES variable even though the file is not when I look in the directory)

file_put_contents($temp_file, file_get_contents($_FILES['file']['tmp_name']));

the I have my temporary file for processing.



回答2:

PHP is ignoring the upload_tmp_dir because of one setting on APPLICATION POOLS. It's not php-cgi.exe nor php.ini or a permissions issue. Go to the application pool of the website experiencing the issue: 1. right click 2. select advanced settings 3. scroll to LOAD USER PROFILE and set it to FALSE.

that did the trick for me.