php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #50177 flock: Example 1 - file is truncated twice
Submitted: 2009-11-14 22:02 UTC Modified: 2009-11-16 11:19 UTC
From: chris at beckr dot org Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Mac / Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: chris at beckr dot org
New email:
PHP Version: OS:

 

 [2009-11-14 22:02 UTC] chris at beckr dot org
Description:
------------
Example 1 of the flock function documentation shows how to lock a file for writing. Once the lock has been obtained, the file is ftruncate()d and date is written.
However, the file is opened using "w", which means that the file will already be truncated even before the lock is issued, which means that any readers (LOCK_SH) that validly obtain a lock in the meantime will see an empty file. Using "r+" instead of "w", everything works as expected. Reproduced on OS X (HFS+) and Linux (ext3).

Reproduce code:
---------------
---
From manual page: function.flock#Examples
---
A sample reader counterpart:
<?php

while (true) {
	$fp = fopen("/tmp/lock.txt", "r");
	
	if (flock($fp, LOCK_SH)) { // open for reading
		$contents = '';

		while (!feof($fp)) {
		   $contents .= fread($fp, 8192);
		}
		
		echo($contents);
	
	    flock($fp, LOCK_UN); // release the lock
	} else {
	    echo "Couldn't get the lock!";
	}

	fclose($fp);
}

?>

Expected result:
----------------
The reader should print only lines with "Write something here"

Actual result:
--------------
The reader sometimes prints empty lines

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-15 20:01 UTC] chris at beckr dot org
Added 'flock' to title
 [2009-11-16 11:19 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2009-11-16 11:20 UTC] svn@php.net
Automatic comment from SVN on behalf of vrana
Revision: http://svn.php.net/viewvc/?view=revision&revision=290804
Log: Fix example (bug #50177)
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sat Oct 10 09:00:01 2026 UTC