如何使用php删除文件的最后一行?

jem*_*990 9 php

我尝试了很多潜在的解决方案,但没有一个能为我工作.最简单的一个:

$file = file('list.html');
array_pop($file);
Run Code Online (Sandbox Code Playgroud)

什么都没做.我在这里做错了吗?它是不同的,因为它是一个HTML文件?

Xav*_*ier 12

这应该有效:

<?php 

// load the data and delete the line from the array 
$lines = file('filename.txt'); 
$last = sizeof($lines) - 1 ; 
unset($lines[$last]); 

// write the new data to the file 
$fp = fopen('filename.txt', 'w'); 
fwrite($fp, implode('', $lines)); 
fclose($fp); 

?>
Run Code Online (Sandbox Code Playgroud)