Reputation: 3920
I have a text file named test
with only 2 lines:
1
2
I want to be able to remove the last line from the file so I use the following function:
<?php
$file = file('test.txt');
array_pop($file);
file_put_contents(implode($file));
?>
For some reason, this does nothing and the file still has the exact lines..am I missing something here?
Upvotes: 0
Views: 3167
Reputation: 106385
file function only returns you the contents of a file (as an array) - and whatever you do with that array, only changes the array, not the file. To persist the changes, write the contents back to the file:
$filename = 'test.txt';
$arr = file($filename);
if ($arr === false) {
die('Failed to read ' . $filename);
}
array_pop($arr);
file_put_contents($filename, implode(PHP_EOL, $arr));
Upvotes: 3