简体   繁体   English

Scandir子文件夹问题,将文件夹和文件循环到单独的阵列中

[英]Scandir subfolder issue, looping through folders and files into separate arrays

building a site for a client who wants to be able to just upload files to their server (with specific naming) and have them parsed for the site content. 为希望将文件仅上传到其服务器(具有特定命名)并对其进行解析以获取网站内容的客户端构建站点。 I got it working great for the first initial folder but I am having a hard time exporting the subfolder files are their own array. 我可以在第一个初始文件夹中很好地工作,但是我很难导出子文件夹文件是它们自己的数组。 Here is the code: 这是代码:

$files = scandir(getcwd());
    foreach ($files as $file) {
        if(is_file($file)) {
            if ($file == 'description.txt') {
                // php less than or equal 5
                $description = file_get_contents('./description.txt', FILE_USE_INCLUDE_PATH);
            }
            if ($file == 'subtitle.txt') {
                // php less than or equal 5
                $subtitle = file_get_contents('./subtitle.txt', FILE_USE_INCLUDE_PATH);
            }
            if ($file == 'item.png') {
                $mainImage = 'item.png';
            }
            if ($file == 'item.jpg') {
                $mainImage = 'item.jpg';
            }
        } elseif (is_dir($file)) {
            $subFiles = scandir($file);
            foreach ($subFiles as $file2) {
                echo $file2;
            }
        }
    }

Problem is that $file2 is including the all of the files from the parent directory instead of just the items from the subfolder. 问题在于$ file2包含父目录中的所有文件,而不仅仅是子文件夹中的项目。 If I echo out $files before the foreach it will also include the subfolder and subfolder files which isnt preferable but doesn't really get in the way of what I want to do either. 如果我在foreach之前回显$ files,它还将包含子文件夹和子文件夹文件,这不是可取的,但实际上并不会妨碍我执行任何操作。 Thanks 谢谢

Scandir also returns "." Scandir也返回“。” and ".." which are also dirs so the else part of you code if(is_file($file)) { is also being executed. 和“ ..”也是dirs,因此代码的else部分if(is_file($ file)){也正在执行。 check and skip "." 检查并跳过“。” and ".." while looping which will solve the problem 和“ ..”同时循环播放即可解决问题

$files = scandir(getcwd());
foreach ($files as $file) {
    if(is_file($file)) {
        if ($file == 'description.txt') {
            // php less than or equal 5
            $description = file_get_contents('./description.txt', FILE_USE_INCLUDE_PATH);
        }
        if ($file == 'subtitle.txt') {
            // php less than or equal 5
            $subtitle = file_get_contents('./subtitle.txt', FILE_USE_INCLUDE_PATH);
        }
        if ($file == 'item.png') {
            $mainImage = 'item.png';
        }
        if ($file == 'item.jpg') {
            $mainImage = 'item.jpg';
        }
    } elseif (is_dir($file)) {
        if($file=="." || $file==".."){
            continue;
        }
        $subFiles = scandir($file);
        foreach ($subFiles as $file2) {
            echo $file2;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM