简体   繁体   English

php scandir()没有显示文件 - 只显示目录

[英]php scandir() not showing files - only showing directories

I have a directory structure as follows: 我有一个目录结构如下:

/files /文件
/files/001 /文件/ 001
/files/001/addfile.php /files/001/addfile.php
/files/002 /文件/ 002
/files/002/deletefile.php /files/002/deletefile.php
/files/003 /文件/ 003
/files/003/viewfile.php /files/003/viewfile.php

My script: 我的剧本:

error_reporting(E_ALL);
$directory      = 'files';
$files      = scandir($directory);

$xml = new DOMDocument('1.0', 'UTF-8');
$xmlFiles = $xml->appendChild($xml->createElement('FILES'));
if(is_dir($directory)) {
    print_r($files);
}
/*foreach($files as $file => $key) {
    if($file == '.' || $file == '..') { continue; }
    echo '$file: '.$file;
    if(is_dir("$directory/$file")) {
        $xmlDir->appendChild($xml->createElement($file));
    }
    else {
        $xmlFiles->appendChild($xml->createElement('FILE', $file));
    }
}*/
echo $xml->saveXML();

My problem: The print_r($files) outputs: 我的问题: print_r($files)输出:

Array ( [0] => . [1] => .. [2] => 001 [3] => 002 [4] => 003 ) 数组([0] =>。[1] => .. [2] => 001 [3] => 002 [4] => 003)

How come scandir only outputs the directories and not the files? 为什么scandir只输出目录而不是文件?

TIA, Nate TIA,Nate

你的文件在这些目录中,而scandir只显示指定路径的内容,但没有任何重复,所以一切都是正确的。

Scandir does not work recursively. Scandir不能递归工作。 It only scans the path input into it. 它只扫描输入到它的路径。

Scandir Recurrsive function to scan deeper into the file system Scandir Recurrsive功能可以更深入地扫描文件系统

you can use RecursiveIteratorIterator for get directory with sub directory listing 您可以将RecursiveIteratorIterator用于带有子目录列表的get目录

Here is how to empty a directory using iterator: 以下是使用迭代器清空目录的方法:

<?php
function empty_dir($dir) {
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
                                             RecursiveIteratorIterator::CHILD_FIRST);
   foreach ($iterator as $path) {
     if ($path->isDir()) {
        // do something with directory
        //    rmdir($dir);
     } else {
       //doo something with files 
       // unlink($path->__toString());
     }
   }
  //    rmdir($dir);
}
?>

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

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