Chi*_*ins 97 php filesystems directory image
我有一个图片目录,我想循环并做一些文件计算.它可能只是缺乏睡眠,但我如何使用PHP查看给定目录,并使用某种for循环遍历每个文件?
谢谢!
Emi*_*röm 250
$files = scandir('folder/');
foreach($files as $file) {
//do your work here
}
Run Code Online (Sandbox Code Playgroud)
或者glob可能更适合您的需求:
$files = glob('folder/*.{jpg,png,gif}', GLOB_BRACE);
foreach($files as $file) {
//do your work here
}
Run Code Online (Sandbox Code Playgroud)
squ*_*rel 61
从该页面上的一条评论:
// output all files and directories except for '.' and '..'
foreach (new DirectoryIterator('../moodle') as $fileInfo) {
if($fileInfo->isDot()) continue;
echo $fileInfo->getFilename() . "<br>\n";
}
Run Code Online (Sandbox Code Playgroud)
递归版本是RecursiveDirectoryIterator.
小智 8
寻找函数glob():
<?php
$files = glob("dir/*.jpg");
foreach($files as $jpg){
echo $jpg, "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)