Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.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_Scandir - Fatal编程技术网

Php 按顺序扫描所有目录和文件

Php 按顺序扫描所有目录和文件,php,scandir,Php,Scandir,我花了(浪费)2天来编写这段糟糕的代码(可能很少出错) $diretc[]=('../documentos'); $txt=”“; 函数getDirContents($diret){ 对于($i=0;$i 0){ getDirContents($directorios); } 返回$txt; } echo getDirContents($diretc); 我试图从我的路径中列出每个目录及其文件documentos我希望所有这些都按顺序进行,就像文件管理器一样,就像树一样试图提供帮助。请尝试以下

我花了(浪费)2天来编写这段糟糕的代码(可能很少出错)

$diretc[]=('../documentos');
$txt=”“;
函数getDirContents($diret){
对于($i=0;$i 0){
getDirContents($directorios);
}
返回$txt;
}
echo getDirContents($diretc);

我试图从我的路径中列出每个目录及其文件documentos我希望所有这些都按顺序进行,就像文件管理器一样,就像树一样试图提供帮助。请尝试以下代码

function listDir($path, $recursive = false) {
    $recursive = ( $recursive === true );
    $invalidPaths = array( '.', '..' );
    $valid = is_dir($path);
    if ($valid !== true) {
        return array();
    }
    $array = array();
    foreach(scandir($path) as $file) {
        // Check for invalid paths
        $invalid = in_array( $file, $invalidPaths );
        if ($invalid === true) {
            continue;
        }
        // Format and add file
        $filename = "$path/$file";
        array_push($array, $filename);

        // Check for read dir recursive
        $isDirectory = is_dir($file);
        if ($isDirectory && !$recursive) {
            continue;
        }

        // Add itens
        $subDirArray = listDir($filename, $recursive);
        $array = array_merge($array, $subDirArray);
    }
    return $array;
}

$path = '../documentos';
$pathList = listDir($path, true);
foreach($pathList as $item) {
    echo "<pre>$item</pre>";
}
函数listDir($path,$recursive=false){ $recursive=($recursive==true); $invalidPaths=数组('.','..); $valid=is_dir($path); 如果($valid!==true){ 返回数组(); } $array=array(); foreach(scandir($path)作为$file){ //检查无效路径 $invalid=在数组中($file,$invalidPath); 如果($invalid==true){ 继续; } //格式化和添加文件 $filename=“$path/$file”; array\u push($array,$filename); //检查read dir递归 $isDirectory=is_dir($file); if($isDirectory&!$recursive){ 继续; } //添加ITEN $subdrarray=listDir($filename,$recursive); $array=array\u merge($array,$subdrarray); } 返回$array; } $path='../documentos'; $pathList=listDir($path,true); foreach($pathlistas$item){ 回显“$item”; }
这到底是怎么回事?用
$txt
替换每个
$GLOBALS['txt']
$diret
应该是字符串而不是array@Ini&Dagon:但是别忘了放
global$txt在您的函数中。@KIKOSoftware无全局needed@Ini:或许可以尝试使用更简单的代码学习PHP?把基本的东西控制住,然后再努力一点?两天内你展示的代码很糟糕。。。我同情你。这对你的心情不好。
function listDir($path, $recursive = false) {
    $recursive = ( $recursive === true );
    $invalidPaths = array( '.', '..' );
    $valid = is_dir($path);
    if ($valid !== true) {
        return array();
    }
    $array = array();
    foreach(scandir($path) as $file) {
        // Check for invalid paths
        $invalid = in_array( $file, $invalidPaths );
        if ($invalid === true) {
            continue;
        }
        // Format and add file
        $filename = "$path/$file";
        array_push($array, $filename);

        // Check for read dir recursive
        $isDirectory = is_dir($file);
        if ($isDirectory && !$recursive) {
            continue;
        }

        // Add itens
        $subDirArray = listDir($filename, $recursive);
        $array = array_merge($array, $subDirArray);
    }
    return $array;
}

$path = '../documentos';
$pathList = listDir($path, true);
foreach($pathList as $item) {
    echo "<pre>$item</pre>";
}