Bug #70704 [Opn->Nab]: fputs
| From: | requinix@php.net | Date: | Tue, 13 Oct 2015 01:20:02 +0000 |
| Subject: | Bug #70704 [Opn->Nab]: fputs | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196584@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70704&edit=1
ID: 70704
Updated by: requinix@php.net
Reported by: euanmee at gmail dot com
Summary: fputs
-Status: Open
+Status: Not a bug
Type: Bug
-Package: Other web server
+Package: Filesystem function related
Operating System: Windows 7 Home Premium
PHP Version: 5.5.30
Block user comment: N
Private report: N
New Comment:
Option a) Use file_put_contents() with the append flag. That is atomic.
Option b) Use file locking to prevent multiple instances of PHP from writing at the same time.
A couple links to get you started:
https://en.wikipedia.org/wiki/File_locking
http://php.net/manual/en/function.flock.php
And this reply wouldn't be complete without an obligatory
Option C) Use a transactional database system instead of flat files.
Previous Comments:
------------------------------------------------------------------------
[2015-10-13 00:58:35] euanmee at gmail dot com
Description:
------------
PHP sometimes only writes part of the string to the disc.
---
From manual page: http://www.php.net/function.fwrite
---
I was originally using Mongoose Web Server - v5.5, with the PHP 5.5.10 supplied with it.
I have now updated to 5.5.30
Three places in my code, I append data to a file. Occasionally, the first part of the write is not
written, i.e. the write is not atomic.
Windows 7 Home Premium, running Mongoose Web Server,using a Western Digital MyBookLive Duo (Debian +
ext4) partition mounted as drive M:
Test script:
---------------
// from getBout.php
$f = fopen("comps/$competitionId/$filename", "a");
// Write the new bout info
fwrite($f, $toWrite);
// Close the -status file
fclose($f);
// from getResults.php
$f = fopen("comps/$competitionId/$filename", "a");
// Write the new bout info
fwrite($f, $toWrite);
// Close the -status file
fclose($f);
// from getScores.php
$f = fopen("comps/$competitionId/$filename", "a");
// Write one bout's results
fwrite($f, $toWrite);
// fwrite($f, "boutScores[" . $currFencer2 . "][" . $currFencer1 . "] =
" . $fencer2Num . ";\r\n") ; // Close the text file
fclose($f);
Expected result:
----------------
// from getBout.php, e.g.
// Bout 2
currBout=2; currFencer1 = 3; currFencer2 = 4;
// from getResults.php, e.g.
///////////////////////////////////////////////////
// Bout 1
currBout=1; boutStartTimes[1] = '2015-10-12T07:47:58.401Z';
currFencer1 = 1; currFencer2 = 2;
boutStarted[1][2] = true ; boutStarted[2][1] = true ;
boutScores[1][2] = 3; boutScores[2][1] = 0;
// from getScores.php, e.g.
///////////////////////////////////////////////////
///////////////////////////////////////////////////
// Bout 1 over
///////////////////////////////////////////////////
currBout=1; boutStartTimes[1] = '2015-10-12T07:47:58.401Z';
currFencer1 = 1; currFencer2 = 2;
boutScores[1][2] = 10; boutScores[2][1] = 0;
boutVictors[1][2] = 'V'; boutVictors[2][1] = 'L';
boutStarted[1][2] = true ; boutStarted[2][1] = true ;
boutStartTimes[1] = '2015-10-12T07:47:58.401Z';
boutFinishTimes[1] = '2015-10-12T07:48:01.587Z';
/////////// /////////////// ///////////
// from an earlier version e.g.
currBout=1; boutStartTimes[1] = '2015-10-12T07:47:58.401Z';
Actual result:
--------------
// from an earlier version e.g.
7:58.401Z';
// 37 characters missing from the intended string, in 0.03% of cases
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70704&edit=1