Search code examples
phpforeachfwritefgets

Variable contents from a file disappears and loop doesnt enter


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?


Solution

  • This part of your code:

    fwrite($val."\n");
    

    Should be:

    fwrite($cache, $val); // the "\n" is only required if it was stripped off after fgets()
    

    The first argument to fwrite() must be a file descriptor opened with fopen().

    Of course, if you had turned on error_reporting(-1) and ini_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 use join() and file_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:

    // to read, assuming the cache file exists
    $cache = include CACHE_FILE_PATH;
    
    // write back cache
    file_put_contents(CACHE_FILE_PATH, '<?php return ' . var_export($cache, true) . ';');
    

    It reads and writes files containing data structures that PHP itself can read (a lot faster than you can).