我试图使用PHP扫描多个图像目录,并创建一个数组,该数组包含每个图像的确切路径及其名称。
示例目录树
main/
├─ sub-directory-1/
│ ├─ image1.png
│ ├─ image2.jpeg
│ ├─ image3.png
├─ sub-directory-2/
│ ├─ image1.png
├─ sub-directory-3/
│ ├─ image1.jpeg
│ ├─ imageX.png所需数组的示例
{
0 : {name: "image1", path: "main/subdirectory-1/image1.png"},
1 : {name: "image2", path: "main/subdirectory-1/image2.jpeg"},
2 : {name: "image3", path: "main/subdirectory-1/image3.png"},
.
.
.
7 : {name: "imageX", path: "main/subdirectory-3/imageX.png"},
}发布于 2022-06-08 13:09:20
这就是给我想要的结果的方法。
我希望这能帮到别人
public function ImageScanner() {
$current_dir = getcwd();
$folderpath = 'PATH/TO/SOME/DIR/';
// Determining if the path is a directory or not
if (is_dir($folderpath)) {
// Opening the directory
$files = opendir($folderpath); {
// Checking if the directory opened
if ($files) {
//Reading each element's name in the directory
while (($subfolder = readdir($files)) !== false) {
// Checking for errors in filename
if ($subfolder != '.' && $subfolder != '..') {
$dirpath = 'SOME/SUB_DIR/PATH/' . $subfolder . '/';
// Checking and opening each file inside the sub directory
if (is_dir($dirpath)) {
$file = opendir($dirpath);
if ($file) {
// Reading each element's name in the sub directory
while (($filename = readdir($file)) !== false) {
if ($filename != '.' && $filename != '..') {
$image_path = '' . $dirpath . '' . $filename . '';
// Creating array for each scanned file
$key = array(
'image_name' => trim($filename) ,
'image_path' => str_replace('/SOME/PATH/', '', $image_path) ,
);
// Appending each image array
$image_arrays[] = $key;
}
}
}
}
}
}
}
}
}
return $image_arrays;
}https://stackoverflow.com/questions/72448388
复制相似问题