Bug #72983 [Com]: file_put_contents does not overwrite if file exists

From: Date: Sun, 04 Sep 2016 02:29:03 +0000
Subject: Bug #72983 [Com]: file_put_contents does not overwrite if file exists
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203771@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72983&edit=1 ID: 72983 Comment by: jmichae3 at yahoo dot com Reported by: jim dot michaels at Jesusnjim dot com Summary: file_put_contents does not overwrite if file exists Status: Not a bug Type: Bug Package: Filesystem function related Operating System: vista x32 amd PHP Version: 7.1.0beta3 Assigned To: cmb Block user comment: N Private report: N New Comment: you are missing the point. even if I had just written "a" then "b" to the file (which seems to work now for some reason), the file would still not that 8 \\\\\\\\\ for 1 \ character is a bell-ringer for a problem that needs fixing. really. I got that down to 4 for 1 it should be 2 when it's in the replace with area shouldn't it? I would rather use extended POSIX regex than PCRE. this bug seems to be fixed now (sort of, except for the 8 \ debacle), and I don't remember replacing php. Previous Comments: ------------------------------------------------------------------------ [2016-09-03 05:45:17] requinix@php.net '\\\\' is interpreted by PHP to be two backslashes, which are then interpreted by PCRE to be a single escaped backslash. You need eight - count 'em EIGHT - backslashes to get two in the output. https://3v4l.org/oLENr ------------------------------------------------------------------------ [2016-09-03 04:43:32] jim dot michaels at Jesusnjim dot com <?php $a="a / b"; $a=preg_replace('/\//','\\',$a); echo file_put_contents("rw.txt", $a); $a=preg_replace('/\//','\\\\',$a); echo file_put_contents("rw.txt", $a); ?> outputs 55 and rw.txt contains a \ b which it should not. no it's not open in an editor. create always is not enabled in the interpreter. 2nd write fails to write to file, same as before. 5==strlen($a). ------------------------------------------------------------------------ [2016-09-02 15:46:32] cmb@php.net Thanks for checking this issue, fernando. I don't know what's wrong in the OP's case, but I suggest to check the return values of all three functions. ------------------------------------------------------------------------ [2016-09-02 15:34:18] fernando at null-life dot com Can't reproduce this issue on a Windows 8 with PHP 7.1 Beta 3 from windows.php.net QA I tried with a local path and with a SMB remote path: <?php error_reporting(E_ALL); file_put_contents('test.txt', "HOLA"); var_dump(file_get_contents('test.txt') == "HOLA"); file_put_contents('test.txt', "ADIOS"); var_dump(file_get_contents('test.txt') == "ADIOS"); file_put_contents('\\\\server\folder\test.txt', "HOLA"); var_dump(file_get_contents('\\\\server\folder\test.txt') == "HOLA"); file_put_contents('\\\\server\folder\test.txt', "ADIOS"); var_dump(file_get_contents('\\\\server\folder\test.txt') == "ADIOS"); D:\fms\php710beta3>php file.php bool(true) bool(true) bool(true) bool(true) ------------------------------------------------------------------------ [2016-09-01 10:30:41] jmichae3 at yahoo dot com yes, error reporting was set for E_ALL. I cannot compile php. requires languages and utilities I do not have. I don't have linux. this is on windows vista. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=72983 -- Edit this bug report at https://bugs.php.net/bug.php?id=72983&edit=1

« previous php.bugs (#203771) next »