Forum Moderators: coopster

Message Too Old, No Replies

Open File, Add Line at Beginning, Remove Last Line

         

Jeremy_H

10:15 pm on Nov 28, 2006 (gmt 0)

10+ Year Member



I'm trying to open a file, write one line at the very beginning, and delete the last line.

Right now I'm stuck at trying to open the file and write a line at the very beginning. Here's my code:

$f="myfile.txt";
fwrite(fopen($f,"a"),"Add This Line\n");

I know I'm opening this file in append mode, where it will always write my text at the end of the document, but I can't figure out any other mode that would allow me to open an existing document and not delete everything that is already in it.

And as far as removing the last line, I have no clue.

Any tips or advice would be much appreciated, thanks.

Little_G

10:30 pm on Nov 28, 2006 (gmt 0)

10+ Year Member



Hi,

This should work, though I haven't checked it!


$f="myfile.txt";
$content = file_get_contents($f);
file_put_contents($f,"Add This Line\n" . substr($content,0,strrpos($content,"\n")));

Andrew

dreamcatcher

10:34 pm on Nov 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jeremy_H,

You need to read the contents of the file, assign the data to a new string, clear the original and then write the new file.


$string = '';
$file = file('myfile.txt');

for ($i=0; $i<count($file); $i++)
{

if ($i!=count($file)-1)
{
$string .= $file[$i];
}

}

$start = 'First Line';

if (file_exists('myfile.txt'))
{
unlink('myfile.txt');
}

$fp = fopen('myfile.txt', 'ab');

if ($fp)
{
fwrite($fp,$start."\n".trim($string));
fclose($fp);
}

Not tested, but something like that should work ok.

dc

[edit] Cheers Andrew. Worth noting that the file_put_contents function will only work in PHP5.

Jeremy_H

1:14 am on Nov 29, 2006 (gmt 0)

10+ Year Member



Thank you.

I was thinking there would be a direct way of adding and removing lines, but knowing this isn't possible is something great to know in the future.

Both your approaches to reading the file and rewriting a modified version worked great, thanks.

I do have two questions:

Is reading and writing files server intensive or scalable?

Also, what would happen if two read/write requests happened at the same time or overlapped? Would this corrupt the file or cause outstanding issues?

Thanks

swa66

4:49 am on Nov 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is reading and writing files server intensive or scalable?

For large files: yes is a heavy operation

Also, what would happen if two read/write requests happened at the same time or overlapped? Would this corrupt the file or cause outstanding issues?

Unpredictable behavior possibly leading to all sorts of problems, use locking to avoid the race condition.

If you need to add at the front of a file, consider other options, such as sorting while viewing, or even storing in different data structure (database comes to mind, but there are other solutions then just grabbing a SQL database as well).