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.
APD TorontoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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.
0
arnoldCommented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

APD TorontoAuthor Commented:
@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?
0
Dave BaldwinFixer of ProblemsCommented:
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?
1
APD TorontoAuthor Commented:
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
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
It turns out that 'php.ini' has a setting for the 'temp directory which is by default the 'system' temp directory.  Change it to something in the web server and it should work better.!
; Directory where the temporary files should be placed.
; Defaults to the system default (see sys_get_temp_dir)
; sys_temp_dir = "/tmp"

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arnoldCommented:
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.
0
APD TorontoAuthor Commented:
Thank you !
1
Dave BaldwinFixer of ProblemsCommented:
You're welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.