Link to home
Start Free TrialLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

Files keep Locking

Hi Experts,

Likely this is a Windows issue, but I am posting under PHP just in case.

I'm trying to use PHP to upload files to  the server.  I'm developing, so the server and the client, are actually the same Win 7 x64 PC with IIS.

Once I upload the files using the following script they get locked in the target directory.
        global $PATH_TO_ROOT;
        $attach_dir = $PATH_TO_ROOT . 'attachments/';
        $client_dir = $attach_dir . 'client-' . $client_id;
        
        //Check if client directory exists
        if (!is_dir($client_dir)){ 
            mkdir($client_dir);
            copy($attach_dir . 'client-0/index.php', $client_dir . '/index.php');  //Security-purposes
        }
        
        //Check if Progress Directory exists
        $progress_dir = $client_dir . '/prog-' . $progress_id;
        if (!is_dir($progress_dir)){ 
            mkdir($progress_dir);
            copy($attach_dir . 'client-0/index.php', $progress_dir . '/index.php');  //Security-purposes
        }
        
        //Upload file
        $temp_file = $_FILES['file']['tmp_name'][$file_no];
        $file = str_replace(' ', '_', $file);
        $dest = $progress_dir . '/' . $file;
        move_uploaded_file($temp_file, $dest);

Open in new window


By locked, I mean that I get access denied both when using <a href=".../myfile.txt">and by trying to opening through the File Explorer. The only way to unlock them is by going into Safe Mode as explained here.

I dont think this is my code, but rather my settings in Windows, but I have posted this under both topics. I am also thinking that the IIS_IUSRS is probably doing upload and maybe that is locking it, but it does have full permissions to the root directory.

Any help will be greatly appreciated.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

I suspect that when you create a new file or directory, it is being created with you as owner and no access by the IIS_IUSRS.  You can right-click on the file name and check the permissions after you create it.
Anti virus on the system seems iis and another process lock it.

Use sysinternals file utils to see if you can identify the resource locking the file.
To test yoour situation, I made this simple demo program.
<?php         //global $PATH_TO_ROOT;
        $attach_dir = 'attachments/';
        $client_dir = $attach_dir . 'client-4';
        
        //Check if client directory exists
        if (!is_dir($client_dir)){ 
            mkdir($client_dir);
            copy($attach_dir . '/index.php', $client_dir . '/index.php');  //Security-purposes
        }
        
        //Check if Progress Directory exists
        $progress_dir = $client_dir . '/prog-' . $progress_id;
        if (!is_dir($progress_dir)){ 
            mkdir($progress_dir);
            copy($attach_dir . 'client-0/index.php', $progress_dir . '/index.php');  //Security-purposes
        }
        exit;
				
        //Upload file
        $temp_file = $_FILES['file']['tmp_name'][$file_no];
        $file = str_replace(' ', '_', $file);
        $dest = $progress_dir . '/' . $file;
        move_uploaded_file($temp_file, $dest);
 ?>

Open in new window

It fails at first because the  IIS_IUSRS user does not have Modify and Write permission, Just Read and Execute.  I get these error messages.
PHP Warning:  mkdir(): Permission denied in C:\inetpub\wwwroot\dibtest\IISperms.php on line 7
PHP Warning:  copy(attachments//index.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\dibtest\IISperms.php on line 8
PHP Notice:  Undefined variable: progress_id in C:\inetpub\wwwroot\dibtest\IISperms.php on line 12
PHP Warning:  mkdir(): No such file or directory in C:\inetpub\wwwroot\dibtest\IISperms.php on line 14
PHP Warning:  copy(attachments/client-0/index.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\dibtest\IISperms.php on line 15

Open in new window

I gave the  IIS_IUSRS  full permissions on the 'attachments' directory and that didn't change anything.  Then I gave the  IIS_IUSRS  full permissions on the Current directory and I got these messages.
PHP Warning:  copy(attachments//index.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\dibtest\IISperms.php on line 8
PHP Notice:  Undefined variable: progress_id in C:\inetpub\wwwroot\dibtest\IISperms.php on line 12
PHP Warning:  copy(attachments/client-0/index.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\dibtest\IISperms.php on line 15

Open in new window


Then I modified the code like this.
<?php         //global $PATH_TO_ROOT;
        $attach_dir = 'attachments/';
        $client_dir = $attach_dir . 'client-4';
        
        //Check if client directory exists
        if (!is_dir($client_dir)){ 
            mkdir($client_dir);
            copy($attach_dir . 'index.php', $client_dir . '/index.php');  //Security-purposes
        }
        
        //Check if Progress Directory exists
        $progress_dir = $client_dir . '/prog-4';
        if (!is_dir($progress_dir)){ 
            mkdir($progress_dir);
            copy($attach_dir . 'index.php', $progress_dir . '/index.php');  //Security-purposes
        }
        exit;
				
        //Upload file
        $temp_file = $_FILES['file']['tmp_name'][$file_no];
        $file = str_replace(' ', '_', $file);
        $dest = $progress_dir . '/' . $file;
        move_uploaded_file($temp_file, $dest);
 ?>

Open in new window

Then it worked to create both the client and the prog directories and copy the index.php file into them.  The permissions of the copied files are the same as the original working directory.  The key here was that the program must have modify/write permissions in the directory it is running in.
Avatar of APD Toronto

ASKER

@Dave - IIS_IUSRS does have full permissions, and I get no errors when  the PHP is creating the directories, copying index.php, or moving the uploaded file.  This all works, and when the script finish executing I see everything in File Explorer, but the issue is I cannot open the uploaded file now.

@arnold - can you please elaborate?
I can open the copied file in my browser.  Did you right-click on the copied file in File Explorer and look at the properties to see what permissions it had?
As you can see, the attachments folder has full control for IIS_IUSRS (so does prog-11 created by php not shown here), but the uploaded file (test - copy.txt) has very limited permissions.

It looks like when php uploads a file it assign limited commissions.  How can I ensure that it gets the same permission as its parent folder?
File-permissions.JPG
Technically, PHP doesn't do the original upload, IIS does.  It may be using a different directory like the Windows 'Temp' directory to do that.  That would be the 'parent' where the file is inheriting it's permissions from.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
https://social.technet.microsoft.com/Forums/office/en-US/074baf4f-1040-43f8-9980-60f273967d8d/which-process-is-locking-a-file?forum=winserverpowershell

This way while on the system you can identify the process locking the file.

If you have anti-virus, it on upload may initiate a scan of the file, or the attributes of the file from the upload have it in a locked state similar to when you download something, look at the file properties and you will see it has an unlock option.

http://www.thewindowsclub.com/fix-windows-blocked-access-file
https://docs.microsoft.com/en-us/sysinternals/downloads/streams
to remove the block on the file.


If it is an anti-virus, exempting the directory from live scanning is the only option to avoid this situation.
Thank you !
You're welcome!