Folder permission not inherited by uploaded file in PHP/IIS

Scenario:

The web application has two components, a private upload area and a public download area. To upload a file you must be logged in via HTTP Authentication in IIS (v6 on Windows 2003). The file is uploaded to a non-web accessible uploads folder. Downloads are public and are allowed for anyone who has the unique file key that identifies a specific uploaded file. The IIS web user (the account that PHP uses) has full access to the uploads directory.

 

Problem:

Files that are uploaded by authorised users cannot be downloaded in the public area.

 

Analysis:

After close examination, it was noticed that the IIS user has no access to the uploaded files even though the user has full access to the uploads directory.

 

Resolution:

By default PHP uploads all files to a directory identified by the upload_tmp_dir entry in php.ini, which on Windows systems defaults to C:\Windows\Temp, when the upload is completed the file is then moved to the target directory as specified by the upload script. The problem is actually with the way Windows handles security, the uploaded file gets the permissions of the Temp directory, when copied to the final directory, it keeps the permissions of the Temp directory and not the permissions of the final directory. That is, if the file is uploaded to Temp and the IIS web user does not have permissions on the Temp folder, when the file is copied to the uploads directory the IIS web user will still NOT have permissions on the file. The solution to the problem is actually quite simple, give the IIS web user the required permissions on the directory specified by upload_tmp_dir.

 

I cannot take credit for this solution, however, I can't find the post that had this solution again. I will keep looking and post the link to the original post when I find it again.

Happy Coding!

Related Posts

This entry was posted in IIS, PHP, Programming, Server and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • JM

    Thanks for this, I was wrestling with this problem for a few days now trying to figure out why my uploaded files weren't inheriting the right permissions. Cheers!

  • thinkinglemur

    I can't take credit for this solution, it took me ages to find the original post now I can't find it again. Glad it helped!

  • Mike

    Thank you! worked like a charm!