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

From: Date: Mon, 05 Sep 2016 04:00:05 +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-203794@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:         requinix@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:

Given that you were able to run PHP just fine earlier, what changed? Are you sure you downloaded the
x86 build?


Previous Comments:
------------------------------------------------------------------------
[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.

------------------------------------------------------------------------
[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.

------------------------------------------------------------------------


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


Thread (15 messages)

« previous php.bugs (#203794) next »