Reputation: 55
I'm trying to edit the first line of a text file via PHP. I have broken my script down and tested the functions 1 by 1. My deletion of line 1 works fine. However I then attempt to insert a line at the beginning and it wipes the file to zero and then writes it.
My code:
<?php
$filename = $_GET['jobname'];
$sunits = $_GET['s'];
$wunits = $_GET['w'];
$funits = $_GET['f'];
$vunits = $_GET['v'];
$tunits = $_GET['t'];
$data = "S: $sunits - W: $wunits - F: $funits - V: $vunits - T: $tunits";
$f = "$filename.txt";
// read into array
$arr = file($f);
// remove second line
unset($arr[0]);
// reindex array
$arr = array_values($arr);
// write back to file
file_put_contents($f,implode($arr));
$handle = fopen("$filename.txt", 'r+') or die('Cannot open file: '.$filename);
fwrite($handle, $data . "\n");
fclose($handle);
?>
Can anyone see what I am doing wrong?
Thanks
Upvotes: 0
Views: 7213
Reputation: 1170
I researched this all night. This was the best solution I could find. Fast, and low resources. Currently, this script will echo the contents. But you could always save to a file. Thought I'd share.
$fh = fopen($local_file, 'rb');
echo "add\tfirst\tline\n"; // add your new first line.
fgets($fh); // moves the file pointer to the next line.
echo stream_get_contents($fh); // flushes the remaining file.
fclose($fh);
Upvotes: 0
Reputation: 1339
I would only use file_get_contents() [file() in your case] + file_put_contents()
.
No need to use fopen() after (it is called when you call file_put_contents()
actually.
<?php
$filename = $_GET['jobname'];
$sunits = $_GET['s'];
$wunits = $_GET['w'];
$funits = $_GET['f'];
$vunits = $_GET['v'];
$tunits = $_GET['t'];
$data = "S: $sunits - W: $wunits - F: $funits - V: $vunits - T: $tunits";
$f = "$filename.txt";
// read into array
$arr = file($f);
// edit first line
$arr[0] = $data;
// write back to file
file_put_contents($f, implode($arr));
?>
you might need to use implode(PHP_EOL,$arr)
so each element of the array is on it's own line
Upvotes: 4
Reputation: 55
What I did in the end was rather than unset $arry[0]
I set $arr[0] = $data . "\n"
and then put back to the file and it is working. Anyone see any problems doing it like this?
Upvotes: 0
Reputation: 3556
You can't add a line at the beginning of a textfile, just at the end. What you have to do is add the new line to the beginning of the array and then write the whole array back:
// Read the file
$fileContents = file('myfile.txt');
// Remove first line
array_shift($fileContents);
// Add the new line to the beginning
array_unshift($fileContents, $data);
// Write the file back
$newContent = implode("\n", $fileContents);
$fp = fopen('myfile.txt', "w+"); // w+ means create new or replace the old file-content
fputs($fp, $newContent);
fclose($fp);
Upvotes: 2
Reputation: 1695
Your issue comes from your use of file_put_contents, which either creates a file if it doesn't exists or wipes the file clean then write contents. In your case you need to use fopen in append mode, fwrite to write the data, then fclose to close the file. There will probably be another step or two in the exact code, but that is the general idea of append data to a file that already exists with content.
Upvotes: 0