简体   繁体   English

从当前目录和子目录中检索所有文件

[英]Retrieve all files from current directory and sub-directories

I am trying to use the following function to retrieve all files in the folder and subfolder, not sure why it returns no result. 我正在尝试使用以下函数来检索文件夹和子文件夹中的所有文件,不确定为什么不返回任何结果。

function listDirectory($path){
    $ret = array();
    function listFolderFiles($dir){
        global $ret;
        if (is_dir($dir) !== true) {
            return false;
        }
        $ffs = scandir($dir);
        unset($ffs[array_search('.', $ffs, true)]);
        unset($ffs[array_search('..', $ffs, true)]);

        foreach($ffs as $ff){
            $ret[] = $ff;
            if(is_dir($ff)) {
                listFolderFiles($dir.'/'.$ff);
            }
        }
        return $ret;
    }
    listFolderFiles($path);
    return $ret;
}

It returns null because the execution of code stops in here 它返回null因为代码的执行在此处停止

if (count($ffs) < 1) {
    return;
}

This is mainly because you've might place a given $path value might be an empty [directory] meaning, no [files] or [directories] at all. 这主要是因为您可能放置了给定的$path值可能是空的[directory],意味着根本没有[files]或[directories]。

Your method listDirectory will return an array if the $path given is actually a directory that contains either a file or directory . 如果给定的$path实际上是包含filedirectory的目录,则方法listDirectory将返回一个array

If you want, You can add another validation before you've called the scandir method first. 如果需要,可以在先调用scandir方法之前添加另一个验证。

function listFolderFiles($dir) {
    global $ret;
    if (is_dir($dir) !== true) {
        return false;
    }

    $ffs = scandir($dir);
    // Rest of code
}

Hope this helps for your case. 希望这对您有帮助。

Thank you very much for your help, sorted. 非常感谢您的帮助。

var_dump(listDirectory("../../../../wp-content/uploads/"));exit;
function listDirectory($path){
    //var_dump($path);exit;
    if(!file_exists ( $path)) {
        var_dump("File doesn't exist");exit;
    }
    $ret = array();
    function listFolderFiles($dir){
        global $ret;
        if (is_dir($dir) !== true) {
            return false;
        }
        $ffs = scandir($dir);
        unset($ffs[array_search('.', $ffs, true)]);
        unset($ffs[array_search('..', $ffs, true)]);
        foreach($ffs as $ff){
            if(is_dir($dir.'/'.$ff)) {
                listFolderFiles($dir.'/'.$ff);
            } else {
                $ret[] = $ff;
            }
        }
        return $ret;
    }
    return listFolderFiles($path);
}

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

相关问题 在特定时间后从目录和所有子目录中修改的PHP删除(.extension)文件 - PHP delete (.extension) files that are modified after specific time from directory and all sub-directories 从目录和子目录中获取数组中的所有图像 - get all images in an array from directory and sub-directories 列出所有使用PHP的子目录和文件 - List all sub-directories and files with PHP PHP递归函数:获取目录及其子目录中的所有文件和目录 - PHP Recursive function: Get all files and directories within a directory and its sub-directories 如何在php中列出目录,子目录和所有文件 - How to list directories, sub-directories and all files in php 如何在子目录中使用子目录名称的前缀重命名所有文件,然后将文件移动到新目录? - How can I rename all files in sub-directories with prefix of sub-directory name and then move files to new directory? PHP:将目录及子目录下的文件列表成一个XML文件 - PHP : list files in directory and sub-directories into an XML file 使用 .htaccess 在目录(包括所有子目录)中禁用 PHP - Disable PHP in directory (including all sub-directories) with .htaccess 所有具有相同名称的文件到数组中(包括子目录) - All files with same name into array (including sub-directories) 目录中带有子目录的多个随机包含 - Multiple Random Includes from Directory with Sub-Directories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM