Jump to content

fread / fwrite code help?


dave_biscuits

Recommended Posts

Hi everyone. Got an issue with a bit of code that I can't figure out:

 

User submits the page with data to save. PHP reads a text file of saved data, updates the data with the changes, writes it back to the text file, then finally re-reads the text file in order to show on screen that the changes have definitely been written properly.

 

The initial read and write works fine - it does definitely save it to the text file no problems - except the re-read after the write doesnt work properly - it keeps displaying on screen the data from the very first read (before changes were made). The result is I have to always refresh the page to get it to read the file again and display the changes properly.

 

My only thought is that PHP only writes to the text file after the code has fully executed - rather than on the fly ...

 

Code is below, any idea what I've done wrong or how I can get around this??

 

$my_array = '';

// If user submitted data, read, make changes, write new data string.

if($_POST['dater']!='') { 

  // READ CODE

    $my_array = '';
    $filename = "../calendar.data";

    if(filesize($filename)!='0') {  
      
      $file = fopen($filename, 'r');
      $theData = fread($file, filesize($filename));
      fclose($file);

      $my_array = '';
      $exploded = explode(',',$theData);
      foreach ($exploded as $explode) {
        if($explode!='') { 
          $e2 = explode('#',$explode);
          $my_array[$e2[0]] = $e2[1];
        }
      }
    
    }

  // INSERT NEW DATA

    $my_array[$_POST['dater']] = $_POST['status'];
    
  // WRITE CODE

    $imploded = '';
    foreach($my_array as $key=>$val) {
      if($val!='') { $imploded = $imploded.rawurlencode($key)."#".rawurlencode($val).","; }
    }

    $filename = "../calendar.data";
    $file = fopen($filename, 'w') or die("can't open file");
    fwrite($file, $imploded);
    fclose($file);

}



// GO FOR MAIN READ

  // RE-READ DATA

    $booked_array = '';
    $myFile = "../calendar.data";

    if(filesize($myFile)!='0') {  
      
      $fh = fopen($myFile, 'r');
      $theData = fread($fh, filesize($myFile));
      fclose($fh);

      $booked_array = '';
      $exploded = explode(',',$theData);
      foreach ($exploded as $explode) {
        if($explode!='') { 
          $e2 = explode('#',$explode);
          $booked_array[$e2[0]] = $e2[1];
        }
      }
    
    }
    

// **** Error: booked_array showing pre-changes array ... }

Link to comment
Share on other sites

PHP uses output buffering and the script would complete before it'd be displayed after writing the file.... I'd recommend using ob_start, flush and sleep() to halt execution of the re-read, I'm not quite in tune with how you coded that.. I can't rewrite it to work for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.