[英]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
实际上是包含file
或directory
的目录,则方法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.