PHP Problem

  Nelmon2k 20:39 13 May 05
Locked

I'm attempting to create my own read and write script to stop me having to directly change the code on my site using FTP. To do this I'm using the following code.

$myfile = fopen ("mytext.txt","r+");
flock ($myfile, LOCK_EX);
fwrite ($myfile, "Random Test Again");
flock ($myfile, LOCK_UN);
fclose ($myfile);

However when I run this it erases the first part of the file similar to what is experienced when using the overwrite command on word. Is there a way to stop this???

  Nelmon2k 21:03 13 May 05

sorted... I've managed to sort it by using the following method if anyone is intrested.

$file = "mytext.txt";
$contents = file_get_contents($file);
$contents = "Add this text to the file".$contents;

$fp = fopen($file,"w");
fwrite($fp,$contents);
fclose($fp);

  harristweed 10:52 14 May 05

change $myfile = fopen ("mytext.txt","r+");

to $myfile = fopen (mytext.txt,'a');

This will (should) open the file in 'append' mode, therefore add to, rather than overrite, i've NOT tested it.

  harristweed 10:54 14 May 05

to $myfile = fwrite (mytext.txt,'a');

  Nelmon2k 11:40 19 May 05

Thanks but the aim of it was to create a script I could use to create a latest news system so it had to be at the top of the script

This thread is now locked and can not be replied to.

Best phone camera 2016/2017: Galaxy S7 vs iPhone 7 vs Google Pixel vs HTC 10 Evo vs OnePlus 3T vs…

1995-2015: How technology has changed the world in 20 years

These are the Best Christmas Ads and Studio Projects of 2016

Super Mario Run preview | Hands-on first impressions of Super Mario Run: Mario's iPhone & iPad…