【问题标题】:PHP file write - max linesPHP 文件写入 - 最大行数
【发布时间】:2012-10-12 11:20:01
【问题描述】:

如何在文件中设置写入限制,如果达到限制则删除最后一行..

例如这里有一个文件:

Line 3
Line 2
Line 1

我只想将它最多排成 3 行.. 所以当我使用任何附加函数编写新行时,它会删除最后一行.. 假设我刚刚写了一个新行(第 4 行).. 所以它去了到最后一个并删除它,结果应该是:

Line 4
Line 3
Line 2

对于新的书面行(第 5 行):

Line 5
Line 4
Line 3

数字行不是必需的,如果通过附加函数 (file_put_contents / fwrite) 有新添加的行,我只想删除最后一行,并将其最大为 3 或我给出的特定数字

【问题讨论】:

    标签: php file append max fopen


    【解决方案1】:

    这是一种方法:

    1. 使用file() 将文件的行读入数组
    2. 使用count() 确定数组中的元素是否超过3 个。如果是这样的话:
      1. array_pop()删除数组的最后一个元素
      2. 使用array_unshift() 将一个元素(新行)添加到数组的前面
      3. 用数组的行覆盖文件

    例子:

    $file_name = 'file.txt';
    
    $max_lines = 3;              #maximum number of lines you want the file to have
    
    $new_line = 'Line 4';               #content of the new line to add to the file
    
    $file_lines = file($file_name);     #read the file's lines into an array
    
    #remove elements (lines) from the end of the
    #array until there's one less than $max_lines
    while(count($file_lines) >= $max_lines) {    
        #remove the last line from the array
        array_pop($file_lines);
    }
    
    #add the new line to the front of the array
    array_unshift($file_lines, $new_line);
    
    #write the lines to the file
    $fp = fopen($file_name, 'w');           #'w' to overwrite the file
    fwrite($fp, implode('', $file_lines)); 
    fclose($fp); 
    

    【讨论】:

    • 通常我永远不会这样做,但你去吧。没有测试过,但这是基本的想法。我最初给了你一个非常清晰的分步过程 - 下次为什么不尝试自己弄清楚,然后如果你卡住了再问另一个问题?这就是学习的方式。
    【解决方案2】:

    试试这个。

    <?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); 
    
    ?>
    

    【讨论】:

    • 它只删除最后一行,我想继续写直到达到最大值,如果达到最大值,则删除最后一行并在顶部添加新行
    • 是的,您可以使用变量 $last 来检查。 $last 包含文件中的行数。因此,如果您的最大值为 3,则可以使用 if($last == 3) { //删除该行并添加新行} else { //添加新行 }
    • 不,不是真的......我没明白,但我得到了正确答案,谢谢
    【解决方案3】:

    你可以试试

    $max = 3;
    $file = "log.txt";
    addNew($file, "New Line at : " . time());
    

    使用的功能

    function addNew($fileName, $line, $max = 3) {
        // Remove Empty Spaces
        $file = array_filter(array_map("trim", file($fileName)));
    
        // Make Sure you always have maximum number of lines
        $file = array_slice($file, 0, $max);
    
        // Remove any extra line 
        count($file) >= $max and array_shift($file);
    
        // Add new Line
        array_push($file, $line);
    
        // Save Result
        file_put_contents($fileName, implode(PHP_EOL, array_filter($file)));
    }
    

    【讨论】:

    • 这个效果很好,但它附加在底部,我想在顶部添加新行,我尝试了 array_unshift 但似乎没有正常工作
    • 您还需要解决方案吗??
    • 没关系,谢谢..如果你有时间,你能把添加到顶部而不是底部吗?
    • 解决方案仅适用于小文件。我不建议将它用于更大的文件,因为整个文件被读入数组(内存消耗)。对于大文件,使用fgets() 来计算行数和fwrite()。以这种方式前置并不优雅但可能:stackoverflow.com/questions/3332262/… 较慢的解决方案,但内存消耗小。
    【解决方案4】:

    修改自Baba's答案;此代码将在文件开头写入新行,并擦除最后一行以始终保持 3 行。

    <?php
    function addNew($fileName, $line, $max) {
        // Remove Empty Spaces
        $file = array_filter(array_map("trim", file($fileName)));
    
        // Make Sure you always have maximum number of lines
        $file = array_slice($file, 0, --$max);
    
        // Remove any extra line and adding the new line
        count($file) >= $max and array_unshift($file, $line);
    
    // Save Result
    file_put_contents($fileName, implode(PHP_EOL, array_filter($file)));
    }
    
    // Number of lines
    $max = 3;
    // The file must exist with at least 2 lines on it
    $file = "log.txt";
    addNew($file, "New Line at : " . time(), $max);
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多