get all the images from a folder in php

前端 未结 9 2147
南方客
南方客 2020-12-04 14:37

I am using WordPress. I have an image folder like mytheme/images/myimages.

I want to retrieve all the images name from the folder myimages<

相关标签:
9条回答
  • 2020-12-04 14:57

    You can simply show your actual image directory(less secure). By just 2 line of code.

     $dir = base_url()."photos/";
    
    echo"<a href=".$dir.">Photo Directory</a>";
    
    0 讨论(0)
  • 2020-12-04 15:04
    //path to the directory to search/scan
            $directory = "";
             //echo "$directory"
            //get all files in a directory. If any specific extension needed just have to put the .extension
            //$local = glob($directory . "*"); 
            $local = glob("" . $directory . "{*.jpg,*.gif,*.png}", GLOB_BRACE);
            //print each file name
            echo "<ul>";
    
            foreach($local as $item)
            {
            echo '<li><a href="'.$item.'">'.$item.'</a></li>';
            }
    
            echo "</ul>";
    
    0 讨论(0)
  • 2020-12-04 15:08

    Here is my some code

    $dir          = '/Images';
    $ImagesA = Get_ImagesToFolder($dir);
    print_r($ImagesA);
    
    function Get_ImagesToFolder($dir){
        $ImagesArray = [];
        $file_display = [ 'jpg', 'jpeg', 'png', 'gif' ];
    
        if (file_exists($dir) == false) {
            return ["Directory \'', $dir, '\' not found!"];
        } 
        else {
            $dir_contents = scandir($dir);
            foreach ($dir_contents as $file) {
                $file_type = pathinfo($file, PATHINFO_EXTENSION);
                if (in_array($file_type, $file_display) == true) {
                    $ImagesArray[] = $file;
                }
            }
            return $ImagesArray;
        }
    }
    
    0 讨论(0)
  • 2020-12-04 15:12

    Check if exist, put all files in array, preg grep all JPG files, echo new array For all images could try this:

    $images=preg_grep('/\.(jpg|jpeg|png|gif)(?:[\?\#].*)?$/i', $files);
    
    
    if ($handle = opendir('/path/to/folder')) {
    
        while (false !== ($entry = readdir($handle))) {
            $files[] = $entry;
        }
        $images=preg_grep('/\.jpg$/i', $files);
    
        foreach($images as $image)
        {
        echo $image;
        }
        closedir($handle);
    }
    
    0 讨论(0)
  • 2020-12-04 15:14

    you can do it simply with PHP opendir function.

    example:

    $handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
    while($file = readdir($handle)){
      if($file !== '.' && $file !== '..'){
        echo '<img src="pictures/'.$file.'" border="0" />';
      }
    }
    
    0 讨论(0)
  • 2020-12-04 15:18
    $dir = "mytheme/images/myimages";
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        $files[] = $filename;
    }
    $images=preg_grep ('/\.jpg$/i', $files);
    

    Very fast because you only scan the needed directory.

    0 讨论(0)
提交回复
热议问题