/** * 删除文件最后一行 * @param $file_path 文件路径 */ public function delLastLine($file_path){ $file = $fp = fopen($file_path, 'r') or die("Unable to open file!"); while(!feof($file)){ $fp = fgets($file); if($fp){ $content[] = $fp; } } array_pop($content); fclose($file); //重新写入文件 $file = fopen($file_path, 'w+'); fwrite($file, implode("", $content)); fclose($file); } 我的文件读取出来之后数组的最后一行保存的是false,所以if($fp)才往数组里写。 如果文件中有空行,请把这个判断去掉,在对数组进行两次删末尾行的操作。 如果太大到超出内存的文件,这个方法不适用。
php删除txt文件最后一行
最新推荐文章于 2021-04-08 08:37:17 发布