Get filenames of images in a directory

后端 未结 4 1865
心在旅途
心在旅途 2020-12-10 16:37

What should be done to get titles (eg abc.jpg) of images from a folder/directory using PHP and storing them in an array.

For example:

a[0] = \'ac.jp         


        
相关标签:
4条回答
  • 2020-12-10 17:08

    It's certainly possible. Have a look at the documentation for opendir and push every file to a result array. If you're using PHP5, have a look at DirectoryIterator. It is a much smoother and cleaner way to traverse the contents of a directory!

    EDIT: Building on opendir:

    $dir = "/etc/php5/";
    
    // Open a known directory, and proceed to read its contents
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            $images = array();
    
            while (($file = readdir($dh)) !== false) {
                if (!is_dir($dir.$file)) {
                    $images[] = $file;
                }
            }
    
            closedir($dh);
    
            print_r($images);
        }
    }
    
    0 讨论(0)
  • 2020-12-10 17:14

    glob in php - Find pathnames matching a pattern

    <?php
        //path to directory to scan
        $directory = "../images/team/harry/";
        //get all image files with a .jpg extension. This way you can add extension parser
        $images = glob($directory . "{*.jpg,*.gif}", GLOB_BRACE);
        $listImages=array();
        foreach($images as $image){
            $listImages=$image;
        }
    ?>
    
    0 讨论(0)
  • 2020-12-10 17:18

    'scandir' does this:

    $images = scandir($dir);
    
    0 讨论(0)
  • 2020-12-10 17:27

    One liner :-

    $arr = glob("*.{jpg,gif,png,bmp}", GLOB_BRACE) 
    
    0 讨论(0)
提交回复
热议问题