我使用此功能查找文件夹和子文件夹中的所有图片:
function rsearch($folder, $pattern) {
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
$fileList = array();
foreach($files as $file) {
$fileList = array_merge($fileList, $file);
}
return $fileList;
}
$list_of_file=rsearch($data_directory,'/^.+\.(jpg|jpeg|png|gif|tiff)$/i');
echo 'longueur '.count($list_of_file);
print_r($list_of_file);
结果如下:
Array
(
[0] => file-user/rep/username/images\asura.jpg
[1] => jpg
[2] => file-user/rep/username/images\beyourself.jpg
[3] => jpg
[4] => file-user/rep/username/images\subfolder\logo3.png
[5] => png
[6] => file-user/rep/username/images\gmail.gif
[7] => gif
)
1)如何将文件名之前的\
更改为/
2)为什么数组会在文件本身之后返回扩展名?怎么预防呢?
答案 0 :(得分:1)
尝试var_dump $文件,你会看到。如果您不想将$ file数组的两个元素放入$ fileList,那么就不要使用array_merge:
foreach($files as $file) {
$fileList[] = $file[0];
}
对于非常粗略和准备好的修复\做一个str_replace或类似的。类似的东西:
foreach($files as $file) {
$fileList[] = str_replace('/','\\',$file[0]);
}