Cannot unlink a file in PHP/Windows
Asked Answered
P

1

3

I'm seeing a few questions raised on this topic, but looking around I have not managed to find a solution, unfortunately.

I'm running PHP5.3 with Apache2 on Windows Server 2008.

A PDF file is created by the script, and after being emailed needs to be deleted, thus:

unlink($filename);

The above code does not delete the file, but returns this error message:

[18-Mar-2013 10:56:06 UTC] PHP Warning: unlink(D:\mypath\temp\myfilename.pdf) [function.unlink]: Permission denied in D:\mypath\Controller\IndexController.php on line 554

Using exec/whoami I can see that the user is "nt authority\system"

Using fileperms function, and parsing it using code in Example2, I can see that the file has these permissions: 33206 -rw-rw-rw-

I have also checked the containing folder, and this system user has Full Control on that folder and its contents.

Could anyone suggest anything else to try to delete the file? Thanks in advance.

Plasticize answered 18/3, 2013 at 11:39 Comment(4)
Are you sure that the file is closed when you call unlink? If the email script still has it open then Windows may not allow you to delete it. Process Monitor ( technet.microsoft.com/en-gb/sysinternals/bb896645 ) would allow you to tell if anything is accessing your file when you are trying to delete it.Cullender
Thanks for the advice! The pdf is created by a separate library and I can see that <code>fclose($filename)</code> is at least called... I will use the file monitor and let know if anything transpires!Plasticize
Play with security - (temporaryly) grant user "Everyone" full control in your temp folder. If that helps, then find out, what permissions should be granted for System. If doesn't help, then there are some antiviruses, which scan file after closing it and may cause such errors. I've seen that (not in web sever scenario however), in these times 100ms delay helped.Manana
Yess! Using the Process Monitor as recommended by glenatron I found the processes that put lock on the file, namely Windows Defender and File Indexing Service. Disabling those allows the file to be deleted. A great debugging tool!Plasticize
D
1

Very first answer should occur in mind when unable to delete: Use by another program or process.

Use flock when writing or performing exclusive operation on file.

Detent answered 1/5, 2013 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.