Jump to content

Loading Two Files At Once With Fopen()


ScrewLooseSalad

Recommended Posts

I'm still a little bit new to PHP, and I want to ammend the beginning of a text file, moving the previous content toward the end; reading around online the conensus I seem to be finding is that this is not possible with the functions as they are, so I plan to move the conents of a first 'master' file to a second 'hold' file, so I can then ammend the master file with the conents of the hold file afterward; however I'm not sure if it is possible to open two files at once; using some sample code I found online I hashed together this piece of code:

 

 /*******************/
 /*  WRITE TO FILE  */
 /*******************/
 /*pass master news file to the hold file*/
 flock($fq, LOCK_EX); //lock file
 fseek($fq,0);
 fwrite($fq, $fp, strlen($fp));
 flock($fq, LOCK_UN); //unlock file
 fclose($fq);

 /*generate new news code*/
 $date = date('jS F Y');
 $outputstring = "<p><em class="newsheader">".$header."</em> - ".$date."\t"."<br>".$newsbody."</p><br>";
 flock($fp, LOCK_EX); //lock file
 fseek($fp,0);
 fwrite($fp, $outputstring, strlen($outputstring));
 flock($fp, LOCK_UN); //unlock file
 fclose($fp);
 echo '<p>News write successful</p>';

 

The master file is replaced fine, however, the hold file ends up containing "Resource id #1", is it not possible to hold open two files at once?

Link to comment
Share on other sites

you are quite right, I've ammended the code to use a variable instead;

however, I'm still getting the same error where the file is being overwritten loosing the old text, and the variable that is supposed to contain the old text seems to hold the value "Resource 1"

any idea where I've gone wrong in my code?

 

ammended code:

 

 /*******************/
 /*    OPEN FILE    */
 /*******************/
 /*open master news file*/
 @ $oldnews = fopen('news.txt',r);
 if (!$oldnews)
 {
  echo '<p>News write failed, couldn't open master file for copying</p>';
  exit;
 }

 /*generate new news code*/
 $date = date('jS F Y');
 $outputstring = "<p><em class="newsheader">".$header."</em> - ".$date."\t"."<br>".$newsbody."</p><br>".$oldnews;

 @ $fp = fopen('news.txt',w);
 if (!$fp)
 {
  echo '<p>News write failed, couldn't open master file for overwriting</p>';
  exit;
 }

 /*******************/
 /*  WRITE TO FILE  */
 /*******************/
 flock($fp, LOCK_EX); //lock file
 fseek($fp,0);
 fwrite($fp, $outputstring, strlen($outputstring));
 flock($fp, LOCK_UN); //unlock file
 fclose($fp);
 echo '<p>News write successful</p>';

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.