8

I have a script that uploads a *.csv to import into a DB table that I have which works great in linux through chmod($target, 0777); but I can't for the life of me find the solution to do exactly this but on a Windows based Apache server.

Some other posts have people responding "don't put in the 0777 and it should work" but that's not the case for me. Thanks!

Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
  • 1
    On Windows it's probably better to set permissions for the Apache user on the upload folder and let permissions inheritance do the rest. If you're having troubles with permissions, this answer may be relevant: http://stackoverflow.com/a/11161766/395384 – EPB May 25 '14 at 06:47

2 Answers2

27

Thanks to the comment left on my original post I was able to figure it out with a little more help from https://web.archive.org/web/20171121192635/http://www.howyoudo.info/index.php/how-to-fix-windows-server-upload-file-inherit-permissions-error/

The problem only happens when you use PHP to upload a file. When you upload a file, PHP sends the file to a temporary directory on the hard drive (for me it is C:\Windows\Temp) and then copies it over to it’s intended directory. Once the file has landed in the temporary directory, it is assigned the permissions of that directory. The problem is when Windows copies that file, it keeps the temporary directory’s permissions and doesn’t inherit your web directory’s permissions.

The easiest way to fix this problem is to add to the temporary directory your intended web directory’s permissions. There’s no need to erase the permissions already in the temporary directory, just add the web directory’s permissions to them. In other words, follow these steps

  1. To change the permissions of your temporary upload directory, find the “upload_tmp_dir” in your php.ini file.
  2. Set it to the directory of your choosing (outside your web folders of course) or leave it at default (for me it is C:\Windows\Temp).
  3. Browse to this folder and add the permissions of your web folders to it.
Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
  • 3
    This is the RIGHT solution for PHP on windows/IIS. – HerrimanCoder Oct 22 '16 at 13:50
  • the issue i had was when a user in the "domain admins" group (yes i know that is bad) uploaded a file, it took the permissions from the temp folder, which caused other users to not be able to see the images. this solution fixed the problem for me, thanks! – Rich701 Aug 30 '17 at 18:20
7

While Brian Leishman's answer will work, if you do not have the ability to edit permissions on the temp folder, you can make your uploaded file inherit permissions from its new location manually with the following on the command line:

icacls "target.txt" /q /c /reset

So, using PHP's exec() function:

exec( 'icacls "target.txt" /q /c /reset' );

For details of the various icacls flags, see: https://technet.microsoft.com/en-us/library/cc753525.aspx

Tip: Using the /t flag, you can use pattern matching to process multiple files.

voidstate
  • 7,937
  • 4
  • 40
  • 52
  • This is a better answer because it's more portable. The other answer relies on extra setup on the server, while this answer could move to a new server and work with less fiddling. – Stephen R Aug 02 '18 at 20:36
  • 1
    Don't forget to run "target.txt" through `escapeshellarg()` first – Stephen R Aug 02 '18 at 20:44