AnswerBun.com

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

Stack Overflow Asked on January 13, 2021

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:

$peer_id=2000000001;
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
$file = fopen($fileLocation,"a+");
fwrite($file, $peer_id);
fclose($file);

Here I read the file:

$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt"; 
$file = fopen($fileLocation,"r");
if(file_exists($fileLocation)){
        // Result is TRUE
}
if(is_readable ($file)){
      // Result is FALSE
}
// an empty variables, because the file is not readable
$peer_id = fread($file);
$peer_id = fileread($file);
$peer_id = file_get_contents($file);
fclose($file);

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

One Answer

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

$peer_id = file_get_contents($fileLocation);

That is the same for is_readable:

if(is_readable($fileLocation)){
    // Result is FALSE
}

So full code should be something like:

$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
if(file_exists($fileLocation) && is_readable($fileLocation)) {
     $peer_id = file_get_contents($fileLocation);
} else {
    echo 'Error message about file being inaccessible here';
}

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:

file_put_contents($fileLocation, $peer_id, FILE_APPEND | LOCK_EX);

Correct answer by user3783243 on January 13, 2021

Add your own answers!

Related Questions

Why does points.sort(function(a, b){return a-b}); return -1, 0 or 1?

6  Asked on November 4, 2021 by camarellini-viberg

 

To add new row in gridview using javascript

2  Asked on November 4, 2021

   

python extract uncompressed data from 7z-file

2  Asked on November 4, 2021 by user3820991

     

JMeter get the latest file / contents from SFTP folder

1  Asked on November 4, 2021 by javaman

         

Pulling Data automatically, and keeping it Dynamic

1  Asked on November 4, 2021 by andrew-ferro

 

Dynamically consolidate columns into one column with a list of values

2  Asked on November 4, 2021 by simon-watson-sjw

 

Validity unknown symbol is not being displayed on PDF

1  Asked on November 4, 2021 by divya-agrawal

     

Use type that isn’t exported

1  Asked on November 4, 2021

 

how to return 0 if no rows

4  Asked on November 4, 2021

   

error installing laravel via composer on new pc

1  Asked on November 4, 2021 by hretic

   

The Data Flows Down in React.js

2  Asked on November 4, 2021 by fahd-dev

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir