diff options
-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; } /** |