Bug #63908 [Opn->Nab]: file_put_contents is not atomic.
Edit report at https://bugs.php.net/bug.php?id=63908&edit=1
ID: 63908
Updated by: rasmus@php.net
Reported by: awm086 at gmail dot com
Summary: file_put_contents is not atomic.
-Status: Open
+Status: Not a bug
Type: Bug
Package: Filesystem function related
Operating System: LINUX
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Sorry, there is no way to guarantee that. Even if we were to check before
starting to write, there is no way to do an atomic check+write, so in between
checking and writing even though the check said there should be space to write to
completion, the filesystem could have filled up by the time we actually do the
write. If you need this, you have to do it in userspace and apply whatever logic
makes sense for your environment to ensure that the write is safe.
Previous Comments:
------------------------------------------------------------------------
[2013-01-04 22:12:57] awm086 at gmail dot com
Description:
------------
The function file_put_contents is not atomic. When the function fails a file
should not be created. However, when the reason for failure is full directory,
(full diskspace, 0 empty), the function return FALSE, but a file is still created
in
the directory with size 0. The function should either complete the job
successfully or have no effects at all (atomic).
Test script:
---------------
$data = 'somedata';
$temp_name = '/tmp/myfile';
if (file_put_contents($temp_name, $data) === FALSE) {
// the message print that the file could not be created.
print 'The file could not be created.';
}
// after this a file named myfile will exist in the directory.
Expected result:
----------------
No file should be created
Actual result:
--------------
File is created in the directory.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63908&edit=1
Thread (10 messages)