scandir()函数返回一个数组,其中包含指定路径中的文件和目录。如下所示:
例子:
复制代码 代码如下:
print_r(scandir('test_directory'));
?>
输出:
复制代码 代码如下:
Array
(
[0]=>.
[1]=>..
[2]=>1.txt
[3]=>2.txt
)
大部分情况只需要该目录的文件列表数组,如下:
复制代码 代码如下:
Array
(
[0]=>1.txt
[1]=>2.txt
)
一般是通过排除“.”或者“..”的数组项解决的:
复制代码 代码如下:
functionfind_all_files($dir)
{
$root = scandir($dir);
foreach($rootas$value)
{
if($value === '.' || $value === '..'){
continue;
}
if(is_file("$dir/$value")){
$result[] = "$dir/$value";
continue;
}
foreach(find_all_files("$dir/$value")as$value)
{
$result[] = $value;
}
}
return$result;
}
?>
另外一种方法,利用array_diff函数,剔除scandir函数执行得到的数组:
复制代码 代码如下:
$directory='/path/to/my/directory';
$scanned_directory=array_diff(scandir($directory),array('..','.'));
?>
通常情况代码管理会产生.svn文件,或者限制目录访问权限的.htaccess等文件。所以通过array_diff函数来过滤会更方便。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.