Skip to content
Advertisement

PHP. How to read a file, if it is writing without a problem with “a+”, but is not readable with “r”?

I have two scripts: one of them writes the value of a variable to a file. In another script, I try to read it. It is written without problems, but it is not readable. Here I write to a file:

JavaScript

Here I read the file:

JavaScript

The code runs on “sprinthost” hosting, if that makes a difference. There are suspicions that this is because of that hosting.

Advertisement

Answer

file_get_contents in short runs the fopen, fread, and fclose. You don’t use a pointer with it. You should just use:

JavaScript

That is the same for is_readable:

JavaScript

So full code should be something like:

JavaScript

The file_get_contents has an inverse function for writing; https://www.php.net/manual/en/function.file-put-contents.php. Use that with the append constant and you should have the same functionality your first code block had:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement