Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:24:33+00:00 2026-06-15T19:24:33+00:00

I have the following code to read from a file, and write back to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:24:35+00:00Added an answer on June 15, 2026 at 7:24 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a code in JAVA like following structure: Read String From
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
i have the following code: <p class=more><a href=/stories class=more>Read more</a></p> and CSS: a.more, a.back
I have following code which reads data from text file and prints to browser.
I have the following code: $SQL = UPDATE jobs SET read = '1' WHERE
I have the following code: f = open(path, 'r') html = f.read() # no
Have a look at the following code you are not required to read the
i executed the following c code on my ubuntu machine...i have read about fcntl()'s
I have following code for loading image from url in xml parsing endElement method
I have got a code snippet as follows: Dim fstream = new filestream(some file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.