diff options
author | Urban Müller | 2016-07-05 17:54:49 +0200 |
---|---|---|
committer | Urban Müller | 2016-07-05 17:54:49 +0200 |
commit | 1af4c9e9c53cb9e76f4b8597ea28163c5f035c26 (patch) | |
tree | a740019b8d779d54e413d7cd69a96bb5a2a35e09 /it.class | |
parent | 2f7e016b7af6aa1a9543dde4cbb369078eef00e1 (diff) | |
download | itools-1af4c9e9c53cb9e76f4b8597ea28163c5f035c26.tar.gz itools-1af4c9e9c53cb9e76f4b8597ea28163c5f035c26.tar.bz2 itools-1af4c9e9c53cb9e76f4b8597ea28163c5f035c26.zip |
use atomic write in it::file_put
Diffstat (limited to 'it.class')
-rw-r--r-- | it.class | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -998,9 +998,9 @@ static function file_get($filename, $p = array()) } /** - * Write data to a file with several serialization modes + * Atomically write data to a file with several serialization modes * @param $filename name of file to write or - for stdout - * @param $data data to write + * @param $data data to write, string by default * @param $p['keyval'] $data must be an assoc array and is written as tab-separated lines * @param $p['lines'] write array of lines, appending newline */ @@ -1011,7 +1011,10 @@ static function file_put($filename, $data, $p = array()) else if ($p['lines']) $data = count((array)$data) ? join("\n", (array)$data) ."\n" : ""; - return ($out = fopen($filename == "-" ? "php://stdout" : $filename, "w")) && (fputs($out, $data) === strlen($data)) && fclose($out); + if (($result = file_put_contents($filename == "-" ? "php://stdout" : "$filename.tmp." . getmypid(), $data)) !== false && $filename != "-") + $result = rename("$filename.tmp." . getmypid(), $filename); + + return $result; } /** |