I have the following code to read from a file, and write back to it after some computation.
if(file_exists(CACHE_FILE_PATH)) {
//read the cache and delete that line!
$inp = array();
$cache = fopen(CACHE_FILE_PATH, 'r');
if($cache) {
while(!feof($cache)) {
$tmp = fgets($cache);
//some logic with $tmp
$inp[] = $tmp;
}
fclose($cache);
}
var_dump($inp);
$cache = fopen(CACHE_FILE_PATH, 'w');
var_dump($inp);
if($cache) {
var_dump($inp);
foreach ($inp as $val) {
echo "\nIN THE LOOP";
fwrite($val."\n");
}
fclose($cache);
}
}
The output of the var_dumps is:
array(3) {
[0]=>
string(13) "bedupako|714
"
[1]=>
string(16) "newBedupako|624
"
[2]=>
string(19) "radioExtension|128
"
}
array(3) {
[0]=>
string(13) "bedupako|714
"
[1]=>
string(16) "newBedupako|624
"
[2]=>
string(19) "radioExtension|128
"
}
array(3) {
[0]=>
string(13) "bedupako|714
"
[1]=>
string(16) "newBedupako|624
"
[2]=>
string(19) "radioExtension|128
"
}
Even though its an array, it is not going in the loop and printing IN THE LOOP! Why?
This part of your code:
Should be:
The first argument to
fwrite()must be a file descriptor opened withfopen().Of course, if you had turned on
error_reporting(-1)andini_set('display_errors', 'On')during development you would have spotted this immediately 🙂As suggested in the comments, you should try to simplify your code by using constructs like
file()to read the whole file into an array of lines and then usejoin()andfile_put_contents()to write the whole thing back.If you just want a cache of key/value pairs, you could look into something like this:
It reads and writes files containing data structures that PHP itself can read (a lot faster than you can).