Search code examples
phpfilefwrite

Open file and write to other file


I need files and write their content to other file. Any ideas how to do it?

I tried the following, but it's not working, the output was only from 1 file not from all

$files = glob('texts/*.txt', GLOB_BRACE);
foreach($files as $file){
  $opn = fopen($file, "r");
  $rad = fread($opn, 1024000);
  fclose($opn);
  $opn = fopen('output.txt', 'a');
  fwrite($opn, $rad);
  fclose($opn);
}

Solution

  • I solved the problem:

    $filesss = fopen('output.txt', 'a');
    if ($handle = opendir('./texts/')) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                $obsah = file_get_contents('./texts/'.$entry);
                fwrite($filesss, $entry.$obsah.'
    ');
            }
        }
        closedir($handle);
    }
    fclose($filesss);
    

    Not be best solution, but for me just. Thansk :)