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

From: Date: Mon, 05 Sep 2016 05:30:10 +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-203796@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 User updated by: jim dot michaels at Jesusnjim 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: why does it seem file_put_contents again does not touch my files? file dates have not changed and menu has not been inserted. yet when I test locally it "works".. so it works sometimes and fails most of the time. does file_put_contents like \ or / in its filepaths, absolute or relative or both? Previous Comments: ------------------------------------------------------------------------ [2016-09-05 04:00:03] requinix@php.net Given that you were able to run PHP just fine earlier, what changed? Are you sure you downloaded the x86 build? ------------------------------------------------------------------------ [2016-09-05 03:21:21] jmichae3 at yahoo dot com c:\>php menu7a.php This version of c:\php71nts\php.exe is not compatible with the version of Windows you're running. Check your computer's system information to see whether y ou need a x86 (32-bit) or x64 (64-bit) version of the program, and then contact the software publisher. c:\> using vista 32, please backrev your compiler. ------------------------------------------------------------------------ [2016-09-04 10:16:37] cmb@php.net > 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); ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ 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 (#203796) next »