Php 如何从一个大文件中获取最后1000行,然后删除它们? 通过两个步骤解决: 获取以下行: 获取线路后,移除线路:

Php 如何从一个大文件中获取最后1000行,然后删除它们? 通过两个步骤解决: 获取以下行: 获取线路后,移除线路:,php,file,Php,File,我有一个大文件,我想从这个文件的末尾取出1000行,然后删除它们 我目前正在使用: function deleteLineInFile($file,$string) { $i=0; $array=array(); $read = fopen($file, "r") or die("can't open the file"); while(!feof($read)) { $array[$i] = fgets($read); ++

我有一个大文件,我想从这个文件的末尾取出1000行,然后删除它们

我目前正在使用:

function deleteLineInFile($file,$string)
{
    $i=0;
    $array=array();

    $read = fopen($file, "r") or die("can't open the file");
    while(!feof($read)) {
        $array[$i] = fgets($read);  
        ++$i;
    }
    fclose($read);

    $write = fopen($file, "w") or die("can't open the file");
    foreach($array as $a) {
        if(!strstr($a,$string)) fwrite($write,$a);
    }
    fclose($write);
}
$goods = '';
$file = file("../products/".$PidFileName);
for ($i = max(0, count($file)-1001); $i < count($file); $i++) {
    $goods = $goods.$file[$i] . '<br />';
    deleteLineInFile("../products/".$PidFileName, $file[$i]);
}
函数deleteLineInFile($file,$string)
{
$i=0;
$array=array();
$read=fopen($file,“r”)或die(“无法打开文件”);
而(!feof($read)){
$array[$i]=fgets($read);
++$i;
}
fclose($read);
$write=fopen($file,“w”)或die(“无法打开文件”);
foreach($a数组){
如果(!strstrstr($a,$string))fwrite($write,$a);
}
fclose($write);
}
$货物='';
$file=文件(“../products/”$PidFileName);
对于($i=max(0,count($file)-1001);$i;
deleteLineInFile(“../products/”$PidFileName,$file[$i]);
}
我想保存我在
$goods


但是,由于文件大小的原因,它会超时。

如果要从EOF中获取N行,可以使用(在PHP5.1中添加):

要在从大文件中获取行后删除行,请执行以下操作: 最好的方法是使用
sed
|但是如果您没有权限使用
exec()
函数,那么这是一个您可以使用的函数

function replace_file($path, $string, $replace)
{
    set_time_limit(0);

    if (is_file($path) === true)
    {
        $file = fopen($path, 'r');
        $temp = tempnam('./', 'tmp');

        if (is_resource($file) === true)
        {
            while (feof($file) === false)
            {
                file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
            }

            fclose($file);
        }

        unlink($path);
    }

    return rename($temp, $path);
}
函数的来源:

要删除该行,请按如下方式使用它:

replace_文件('myfile.txt','removethissplease',''

  • 如果您使用了MrSmile的答案来获取行,则将“RemoveThisPlease”替换为
    $line

是否从浏览器或命令行运行它?@user3783243-浏览器。请检查以下答案:。是否需要从浏览器运行它?命令行没有超时。在这种情况下,我建议使用DB。使用这种方法可能会耗尽内存,并处理文件系统锁。非常感谢!但是有可能保存到数据库中吗?我认为在foreach循环期间,
$line
是我应该保存到数据库中的,对吗?@MatrixCow08,是的!如果要在foreach循环期间立即保存到DB。因此,您甚至不需要打开新文件,但它仍然有1467行,而不是我在$num_to_cut中输入的行。。。我确保我输入的数字是整数,不是字符串,而是相同的。@MatrixCow08,您已经有多少行了,要剪切多少行?
function replace_file($path, $string, $replace)
{
    set_time_limit(0);

    if (is_file($path) === true)
    {
        $file = fopen($path, 'r');
        $temp = tempnam('./', 'tmp');

        if (is_resource($file) === true)
        {
            while (feof($file) === false)
            {
                file_put_contents($temp, str_replace($string, $replace, fgets($file)), FILE_APPEND);
            }

            fclose($file);
        }

        unlink($path);
    }

    return rename($temp, $path);
}