|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51227 You cannot delete a locked file on Windows
Submitted: 2010-03-07 03:05 UTC Modified: 2010-11-22 06:21 UTC
From: whistl0r+php at googlemail dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.2.13 OS: win32 only
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: whistl0r+php at googlemail dot com
New email:
PHP Version: OS:


 [2010-03-07 03:05 UTC] whistl0r+php at googlemail dot com
A script creates files. To make sure that no "reader" is accessing the file while 
the script is changing its content (think of a cache!), you will use flock() to 
secure the progress.

Tested with PHP Version 5.2.12 (CGI/FastCGI), latest Zend Server release.

Test script:
// Set a plain text header
header('Content-Type: text/plain; charset=iso-8859-1');
header('X-Content-Type-Options: nosniff');

$tempFile = tempnam(sys_get_temp_dir(), 'lTest');
if ($tempFile === FALSE)
	die('Temp file creation failed!');
	echo 'Our temp file will be: ' . $tempFile . PHP_EOL;

$fh = fopen($tempFile, 'w');
if ($fh === FALSE)
	die('Cannot get file handler!');

$lock = flock($fh, LOCK_EX);
if ($lock === FALSE)
	die('Cannot get file lock!');


if (file_exists($tempFile) === TRUE)
	die('I was unable to delete the file!');

$lock = flock($fh, LOCK_UN);
if ($lock === FALSE)
	die('Unable to release lock!');


echo 'Successful! We created a temp file, which we deleted, while the file was locked!' . PHP_EOL;

Expected result:
I expect that it will work in Windows, like it is working on Linux.
If this isn't possible, it should be documented.

Actual result:
It will work fine on Linux, but on Windows it will raise a "SHARING VIOLATION" 
(trace it in "Process Monitor" from SysInternals).


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-03-07 03:07 UTC] whistl0r+php at googlemail dot com
Tested on Windows 7, x64.
 [2010-03-10 22:11 UTC]
-Operating System: Windows +Operating System: win32 only
 [2010-11-22 06:21 UTC]
-Status: Open +Status: Bogus
 [2010-11-22 06:21 UTC]
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

This is documented in flock(): "flock() uses mandatory locking instead of advisory locking on Windows"
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 21 21:01:31 2024 UTC