【问题标题】:merge all files in directory to one text file将目录中的所有文件合并为一个文本文件
【发布时间】:2013-06-06 21:19:26
【问题描述】:

在 PHP 中,如何打开目录中的每个文件、所有文本文件并将它们全部合并到一个文本文件中。

我不知道如何打开目录中的所有文件,但我会使用 file() 命令打开下一个文件,然后使用 foreach 将每一行附加到数组中。 像这样:

$contents = array();
$line = file(/*next file in dir*/);
foreach($lines as line){
   array_push($line, $contents);
}

然后我会将该数组写入一个新的文本文件,我在目录中没有更多文件。

如果您有更好的方法,请告诉我。

或者如果你能帮我实现我的解决方案,尤其是在目录中打开下一个文件,请告诉我!

【问题讨论】:

标签: php file


【解决方案1】:

OrangePill 的答案是错误的。

它返回一个空文件和一个编译错误。 问题是他使用 fread(读取字节)而不是 fget(读取行)

这是有效的答案:

  //File path of final result
    $filepath = "mergedfiles.txt";

    $out = fopen($filepath, "w");
    //Then cycle through the files reading and writing.

      foreach($filepathsArray as $file){
          $in = fopen($file, "r");
          while ($line = fgets($in)){
                print $file;
               fwrite($out, $line);
          }
          fclose($in);
      }

    //Then clean up
    fclose($out);

    return $filepath;

享受吧!

【讨论】:

    【解决方案2】:

    你这样做的方式会消耗大量内存,因为它必须将所有文件的内容保存在内存中......这种方法可能会更好一些

    首先得到你想要的所有文件

      $files = glob("/path/*.*");
    

    然后打开一个输出文件句柄

      $out = fopen("newfile.txt", "w");
    

    然后循环读取和写入文件。

      foreach($files as $file){
          $in = fopen($file, "r");
          while ($line = fread($in)){
               fwrite($out, $line);
          }
          fclose($in);
      }
    

    然后清理

      fclose($out);
    

    【讨论】:

    • 谢谢,你能帮我做两件事吗?在关闭文件之前,如何删除任何重复的单词/行(每行包含一个单词),然后按字母顺序排列这些行?
    • 如果您在执行上述操作后在 unix/linux 机器上,您可以通过exec("sort newfile.txt | uniq > newfile.sorted.txt'");运行它
    • 我刚刚尝试了脚本,它正在编写一个空白文本文件。文本文件中没有任何内容。这是我的 index.php 文件中的代码:<?php $files = glob("/*.*"); $out = fopen("listTogether.txt", "w"); foreach($files as $file){ $in = fopen($file, "r"); while($line = fread($in)){ fwrite($out, $line); } fclose($in); } fclose($out); ?>
    • /*.* 必须匹配网络服务器可读的文件掩码。我很确定根文件系统不是。如果你想读取当前目录中以 .txt 结尾的文件,那么你可以传入./*.txt
    • 如果我不知道文件扩展名怎么办。嗯,我知道。有多个文件扩展名。他们是:.10, .50, .60, .70, .80, .95, .20, .35, .55, .40, .0, .1, .2, .txt, .30, and .90
    【解决方案3】:

    试试这个:

    <?php
    //Name of the directory containing all files to merge
    $Dir = "directory";
    
    
    //Name of the output file
    $OutputFile = "filename.txt";
    
    
    //Scan the files in the directory into an array
    $Files = scandir ($Dir);
    
    
    //Create a stream to the output file
    $Open = fopen ($OutputFile, "w"); //Use "w" to start a new output file from zero. If you want to increment an existing file, use "a".
    
    
    //Loop through the files, read their content into a string variable and write it to the file stream. Then, clean the variable.
    foreach ($Files as $k => $v) {
        if ($v != "." AND $v != "..") {
            $Data = file_get_contents ($Dir."/".$v);
            fwrite ($Open, $Data);
        }
        unset ($Data);
    }
    
    
    //Close the file stream
    fclose ($Open);
    ?>
    

    【讨论】:

      【解决方案4】:

      试试下面的代码,享受吧!!!

      /* Directory Name of the files */
      $dir = "directory/subDir";
      /* Scan the files in the directory */
      $files = scandir ($dir);
      /* Loop through the files, read content of the files and put then OutFilename.txt */
      $outputFile = "OutFilename.txt";
      foreach ($files as $file) {
          if ($file !== "." OR $file != "..") {
              file_put_contents ($outputFile, file_get_contents ($dir."/".$file),  FILE_APPEND);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-06-03
        • 1970-01-01
        • 2019-03-03
        • 1970-01-01
        • 2014-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多