Hello
in myfile.txt I have
house
name
car
If I execute this
file_put_contents(“myfile.txt”, str_replace(“name”,“”,file_get_contents(“myfile.txt”) ));
myfile became empty !!
Anyone can explain me why ?
Thank you
Hello
in myfile.txt I have
house
name
car
If I execute this
file_put_contents(“myfile.txt”, str_replace(“name”,“”,file_get_contents(“myfile.txt”) ));
myfile became empty !!
Anyone can explain me why ?
Thank you
Be careful because [fphp]file_put_contents[/fphp] empties the file before writing, so if you made a mistake, future calls will have file_get_contents receiving an empty file.
You can always check to see what file_get_contents is getting by separating it out.
$newContent = str_replace("name","",file_get_contents("myfile.txt") );
file_put_contents("myfile.txt", $newContent);
thank you but it’s the same , the file became empty . As it seems it’s an issue with linux permission , becuase if I set the file in another location it always works correctly.
Even if I do not understand why the file goes empty only in this file location , this make me stressed for hours .
That doesn’t make any sense at all. If you don’t have permission, you can’t empty the file…
Chances are the file is still empty from an earlier attempt and it needs to have its original data reloaded to it.
I agree, it doesn’t make sense but it’s happening ! I lost all morning on this, I use file_put_contents often and I never had this problem .
Finally I decided to save the file in another location and it works as usual.
Did you remember to re-enter the “house/name/car” into the file after you found it was empty? Otherwise, the second time it runs the script finds the contents of the file to be “” (empty), and… replaces it with “” (empty).