【问题标题】:Append at the beginning of the file in PHP [duplicate]在PHP文件的开头追加[重复]
【发布时间】:2013-06-03 09:56:06
【问题描述】:

您好,我想使用 php 在文件开头追加一行。

例如,该文件包含以下内容:

    Hello Stack Overflow, you are really helping me a lot.

现在我想像这样在前一行的顶部添加一行:

    www.stackoverflow.com
    Hello Stack Overflow, you are really helping me a lot.

这是我目前在脚本中的代码。

    $fp = fopen($file, 'a+') or die("can't open file");
    $theOldData = fread($fp, filesize($file));
    fclose($fp);

    $fp = fopen($file, 'w+') or die("can't open file");
    $toBeWriteToFile = $insertNewRow.$theOldData;
    fwrite($fp, $toBeWriteToFile);
    fclose($fp);

我想要一些最佳解决方案,因为我在 php 脚本中使用它。以下是我在这里找到的一些解决方案: Need to write at beginning of file with PHP

在开头附加以下内容:

    <?php
    $file_data = "Stuff you want to add\n";
    $file_data .= file_get_contents('database.txt');
    file_put_contents('database.txt', $file_data);
    ?>

还有一个在这里: Using php, how to insert text without overwriting to the beginning of a text file

这样说:

    $old_content = file_get_contents($file);
    fwrite($file, $new_content."\n".$old_content);

所以我的最后一个问题是,在上述所有方法中,哪种方法是最好的(我的意思是最优的)。还有比上面更好的吗?

寻找您对此的想法!!!。

【问题讨论】:

  • 来自手册If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
  • @Robert,我的问题是,这是最好的方法。请完整阅读。我还提到了您在问题中可能重复的问题。
  • 文件是否也在 PHP 中读取? (生成的文件是否在稍后被处理以供 PHP 读取?)
  • file_get_contents 可能需要读取内存中的完整文件..如何像'w+'一样逐部分读取它?
  • @ring0,我不太明白你的问题。我只用 php 读写。

标签: php


【解决方案1】:
function file_prepend ($string, $filename) {

  $fileContent = file_get_contents ($filename);

  file_put_contents ($filename, $string . "\n" . $fileContent);
}

用法:

file_prepend("couldn't connect to the database", 'database.logs');

【讨论】:

    【解决方案2】:

    你的意思是前置。我建议您阅读该行并将其替换为下一行而不会丢失数据。

    <?php
    
    $dataToBeAdded = "www.stackoverflow.com";
    $file = "database.txt"; 
    
    $handle = fopen($file, "r+");
    
    $final_length = filesize($file) + strlen($dataToBeAdded );
    
    $existingData = fread($handle, strlen($dataToBeAdded ));
    
    rewind($handle);
    
    $i = 1;
    
    while (ftell($handle) < $final_length) 
    {
    
      fwrite($handle, $dataToBeAdded );
    
      $dataToBeAdded  = $existingData ;
    
      $existingData  = fread($handle, strlen($dataToBeAdded ));
    
      fseek($handle, $i * strlen($dataToBeAdded ));
    
      $i++;
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      在文件的第一行之前没有真正有效的方法。您的问题中提到的两种解决方案都通过从旧文件中复制所有内容然后写入新数据来创建一个新文件(这两种方法之间没有太大区别)。

      如果您真的追求效率,即避免现有文件的整个副本,并且您需要将最后插入的行作为文件中的第一行,这完全取决于您在创建文件后打算如何使用文件.

      三个文件

      根据您的评论,您可以创建三个文件headercontentfooter,并依次输出它们;即使header 是在content 之后创建的,也会避免复制。

      在一个文件中反向工作

      此方法将文件放入内存(数组)中。
      因为你知道你在页眉之前创建内容,所以总是以相反的顺序行,页脚,内容,然后是页眉:

      function write_reverse($lines, $file) { // $lines is an array
         for($i=count($lines)-1 ; $i>=0 ; $i--) fwrite($file, $lines[$i]);
      }
      

      然后您首先使用页脚调用write_reverse(),然后是内容,最后是标题。每次你想在文件的开头添加一些东西,只要写在末尾...

      然后读取文件进行输出

      $lines = array();
      while (($line = fgets($file)) !== false) $lines[] = $line;
      
      // then print from last one
      for ($i=count($lines)-1 ; $i>=0 ; $i--) echo $lines[$i];
      

      还有另一个考虑因素:你能完全避免使用文件吗?例如通过PHP APC

      【讨论】:

      • 好主意。但是,如果我使用这个function write_reverse($lines, $file) { // $lines is an array for($i=count($lines)-1 ; $i&gt;=0 ; $i--) fwrite($file, $lines[$i]); },这将消耗时间,因为它会重复文件的每一行,而不是当前的解决方案一次将整个数据写入文件。
      • 这取决于您最初创建文件的方式(这不是问题的一部分)。 write_reverse 仅在创建或扩展文件时执行一次。当然,解释 PHP 中的逐行操作比唯一操作要慢一些 - 但是使用 write_reverse 在添加诸如标题之类的内容时,您不会复制整个文件......只有新行。这会有所不同,尤其是在文件很大的情况下。
      【解决方案4】:

      我个人在写入文件时的偏好是使用file_put_contents

      来自手册:

      此函数与调用 fopen()、fwrite() 和 fclose() 相同 依次将数据写入文件。

      因为该函数会自动为我处理这三个函数,所以我不必记住在完成后关闭资源。

      【讨论】:

      • 程序员...总是懒惰 :D
      猜你喜欢
      • 2021-12-13
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2012-07-12
      • 1970-01-01
      相关资源
      最近更新 更多