Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php编写子目录';将内容放入单独的文本文件中_Php - Fatal编程技术网

php编写子目录';将内容放入单独的文本文件中

php编写子目录';将内容放入单独的文本文件中,php,Php,我试图列出子目录中的文件,并将这些列表写入单独的文本文件中 我设法得到了目录和子目录列表,甚至把所有的文件都写到了一个文本文件中 我只是似乎没有设法打破我正在创造的循环。我要么以一个文本文件结束,要么第二个+文件也包括所有前面的子目录内容 我需要实现的是: 目录A/AA/a1.txt,a2.txt>>AA.log 目录A/BB/b1.txt,b2.txt>>BB.log 等等 希望这是有意义的 我发现了递归DirectoryIterator方法,如中所述,非常有帮助。然后,我使用for和fo

我试图列出子目录中的文件,并将这些列表写入单独的文本文件中

我设法得到了目录和子目录列表,甚至把所有的文件都写到了一个文本文件中

我只是似乎没有设法打破我正在创造的循环。我要么以一个文本文件结束,要么第二个+文件也包括所有前面的子目录内容

我需要实现的是:

  • 目录A/AA/a1.txt,a2.txt>>AA.log
  • 目录A/BB/b1.txt,b2.txt>>BB.log
  • 等等
希望这是有意义的


我发现了递归DirectoryIterator方法,如中所述,非常有帮助。然后,我使用for和
foreach
循环遍历目录,写入文本文件,但我无法将它们分解为多个文件。

很可能您没有过滤出目录

$maindir=opendir('A');
if (!$maindir) die('Cant open directory A');
while (true) {
  $dir=readdir($maindir);
  if (!$dir) break;
  if ($dir=='.') continue;
  if ($dir=='..') continue;
  if (!is_dir("A/$dir")) continue;
  $subdir=opendir("A/$dir");
  if (!$subdir) continue;
  $fd=fopen("$dir.log",'wb');
  if (!$fd) continue;
  while (true) {
    $file=readdir($subdir);
    if (!$file) break;
    if (!is_file($file)) continue;
    fwrite($fd,file_get_contents("A/$dir/$file");
  }
  fclose($fd);
}

我想我应该用另一种方式来演示,因为这似乎是一个很好的使用场所


我不太明白你到底有什么问题。我通常会说这样的问题是可以解决的,但是您给出的描述为变量留出了太多的空间,因此我认为您应该改进您的问题。请提供您遇到问题的源代码。感谢您迄今为止的回复!我将通过以上工作和天气解决或不我会带回源代码。我以前没有这样做的唯一原因是我尝试了多种方法,但总是以同样的方式失败。从上面我看到的第一件事是,我的想法可能不正确,我可能使用了错误的循环类型。很快回来,再次感谢!!大家好,我要再次向大家表示感谢——我应该向大家汇报(我想),但这是一个家庭项目,在过去的一周里,工作完全超过了生活,我还没有搞定,但要尽快回来!再次感谢你的帮助!
// Where to start recursing, no trailing slash
$start_folder = './test';
// Where to output files
$output_folder = $start_folder;

chdir($start_folder);

function glob_each_dir ($start_folder, $callback) {

    $search_pattern = $start_folder . DIRECTORY_SEPARATOR . '*';

    // Get just the folders in an array
    $folders = glob($search_pattern, GLOB_ONLYDIR);

    // Get just the files: there isn't an ONLYFILES option yet so just diff the
    // entire folder contents against the previous array of folders
    $files = array_diff(glob($search_pattern), $folders);

    // Apply the callback function to the array of files
    $callback($start_folder, $files);

    if (!empty($folders)) {
        // Call this function for every folder found
        foreach ($folders as $folder) {
            glob_each_dir($folder, $callback);
        }
    }
}

glob_each_dir('.', function ($folder_name, Array $filelist) {
        // Generate a filename from the folder, changing / or \ into _
        $output_filename = $_GLOBALS['output_folder']
            . trim(strtr(str_replace(__DIR__, '', realpath($folder_name)), DIRECTORY_SEPARATOR, '_'), '_')
            . '.txt';
        file_put_contents($output_filename, implode(PHP_EOL, $filelist));
    });