首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫描PHP中的多个图像目录

扫描PHP中的多个图像目录
EN

Stack Overflow用户
提问于 2022-05-31 13:08:42
回答 1查看 33关注 0票数 -1

我试图使用PHP扫描多个图像目录,并创建一个数组,该数组包含每个图像的确切路径及其名称。

示例目录树

代码语言:javascript
复制
main/
├─ sub-directory-1/
│  ├─ image1.png
│  ├─ image2.jpeg
│  ├─ image3.png
├─ sub-directory-2/
│  ├─ image1.png
├─ sub-directory-3/
│  ├─ image1.jpeg
│  ├─ imageX.png

所需数组的示例

代码语言:javascript
复制
{
    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"},
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-08 13:09:20

这就是给我想要的结果的方法。

我希望这能帮到别人

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72448388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档