且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何打开文件并删除最后一行?

更新时间:2023-08-28 17:12:34

在文件系统上处理文件的简单方法:

A simple way with the file on the filesystem:

<?php

$path = "file.txt";
$content = file($path); // Parse file into an array by newline
$data = array_pop($content);

if (trim($data) == '?>') {
    $content[] = 'echo "... again";';
    $content[] = "\n$data";
    file_put_contents($path, implode($content));
}

是哪个.

$ cat file.txt 
<?php
echo 'Hello world';
?>
$ php test.php 
$ cat file.txt 
<?php
echo 'Hello world';
echo "... again";
?>