PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » PHP » PHP_Compat » Bug #6082

[patch] file_put_contents with locking support

Details

Request #6082[patch] file_put_contents with locking support
Submitted2005-11-26 19:21 UTC
Fromjudas_iscariote at imapmail dot org
Assignedaidan
StatusClosed
PackagePHP_Compat
PHP VersionIrrelevant
OSIrrelevant
Roadmaps(Not assigned)

Comments

[2005-11-26 19:21 UTC] judas_iscariote at imapmail dot org

Description:
------------
file_put_contents does't support locking (ie . LOCK_EX)
suggested code attached...

Test script:
---------------
ndex: file_put_contents.php
===================================================================
--- file_put_contents.php (revisión: 45)
+++ file_put_contents.php (copia de trabajo)
@@ -26,6 +26,9 @@
define('FILE_APPEND', 8);
}

+if (!defined('LOCK_EX')) {
+ define ('LOCK_EX', 2);
+}

/**
* Replace file_put_contents()
@@ -60,13 +63,15 @@
// Check what mode we are using
$mode = ($flags & FILE_APPEND) ?
'a' :
- 'w';
+ 'wb';

// Check if we're using the include path
$use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ?
true :
false;

+ $use_lock = ($flags & LOCK_EX) ? true : false ;
+
// Open the file for writing
if (($fh = @fopen($filename, $mode, $use_inc_path)) === false) {
user_error('file_put_contents() failed to open stream: Permission denied',
@@ -76,6 +81,16 @@

// Write to the file
$bytes = 0;
+
+ if($use_lock == true ) {
+
+ if (!flock($fh, LOCK_EX)) { // do an exclusive lock
+
+ return false;
+ }
+
+ }
+
if (($bytes = @fwrite($fh, $content)) === false) {
$errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s',
$length,
@@ -84,8 +99,8 @@
return false;
}

- // Close the handle
- @fclose($fh);
+ // Close the handle (lock released here too )
+ @fclose($fh);

// Check all the data was written
if ($bytes != $length) {
@@ -101,4 +116,4 @@
}
}

-?>
\ No newline at end of file
+?>

Expected result:
----------------
locking implemented

Actual result:
--------------
locking not implemented