'merge all files in directory to one text file

In PHP, how can I open everyfile, all text files, in a directory and merge them all into one text file.

I don't know how to open all files in a directory, but I would use the file() command to open the next file and then a foreach to append each line to an array. like so:

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

Then I would write that array to a new text file one I've reached no more files in the directory.

if you have a better way of doing this then please let me know.

Or if you can help me implement my solution especially the opening the next file in the dir, please let me know!



Solution 1:[1]

OrangePill's answer is WRONG.

It returns an empty file and a compile ERROR. The problem was that he used fread (reads bytes) instead of fget (reads lines)

This is the working answer:

  //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;

Enjoy!

Solution 2:[2]

The way that you are doing it is going to consume a lot of memory because it has to hold the contents of all of the files in memory ... this approach may be a little better

First off get all of the files you are going to want

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

Then open an output file handle

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

Then cycle through the files reading and writing.

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

Then clean up

  fclose($out);

Solution 3:[3]

Try this:

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

Solution 4:[4]

Try below code and enjoy!!!

/* 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);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rod
Solution 2 Orangepill
Solution 3
Solution 4 Ariful Islam Rajib