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

From: Date: Sun, 04 Sep 2016 10:16:38 +0000
Subject: Bug #72983 [Nab]: file_put_contents does not overwrite if file exists
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203780@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 Updated by: cmb@php.net 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: > No bug. There is exactly one bug in this bug report and it is > with your own code. Not with PHP. Not with PCRE. That. Suggestion: use str_replace() instead of preg_replace() in this case: str_replace('/', '\\', $a); Previous Comments: ------------------------------------------------------------------------ [2016-09-04 02:50:58] requinix@php.net Actually you're the one missing the point. There is no bug. The problem is that your code was not doing what you thought it was doing. It has nothing to do with files and everything to do with you not understanding what was happening with backslashes. https://3v4l.org/C361B See how the output in both versions is the same? Writing to the file worked fine, but you were writing the same value both times. "that 8 \\\\\\\\\ for 1 \ character is a bell-ringer for a problem that needs fixing" I'm not sure how you managed to misunderstand my comment, but between PHP and PCRE you need two levels of escaping. That's 4x the number of backslashes required. One in the output = 4 in the string, two in the output = 8 in the string. Side note: here's what happens when you use 1-8 backslashes in PHP and PHP+PCRE: https://3v4l.org/Sjn85 "//bug in PCRE doesn't replace / with 2 \ chars" No bug. There is exactly one bug in this bug report and it is with your own code. Not with PHP. Not with PCRE. ------------------------------------------------------------------------ [2016-09-04 02:33:56] jmichae3 at yahoo dot com am now using this code. <?php $a="a / b\r\n"; echo file_put_contents("rw.txt", $a); $a=preg_replace('/\//','\\\\', $a);//bug in PCRE doesn't replace / with 2 \ chars echo $a; echo file_put_contents("rw.txt", $a); system("dir rw.txt"); system("type rw.txt");//should be a \\ b ?> and this does overwrite like it's supposed to. ------------------------------------------------------------------------ [2016-09-04 02:28:59] jmichae3 at yahoo dot com 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. ------------------------------------------------------------------------ [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). ------------------------------------------------------------------------ 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 (#203780) next »