Pull all images from a specified directory and then display them

前端 未结 5 959
孤街浪徒
孤街浪徒 2020-11-29 19:33

How to display the image from a specified directory? like i want to display all the png images from a directory, in my case my directory is media/images/iconized.

I t

相关标签:
5条回答
  • 2020-11-29 19:53

    In case anyone is looking for recursive.

    <?php
    
    echo scanDirectoryImages("images");
    
    /**
     * Recursively search through directory for images and display them
     * 
     * @param  array  $exts
     * @param  string $directory
     * @return string
     */
    function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
    {
        if (substr($directory, -1) == '/') {
            $directory = substr($directory, 0, -1);
        }
        $html = '';
        if (
            is_readable($directory)
            && (file_exists($directory) || is_dir($directory))
        ) {
            $directoryList = opendir($directory);
            while($file = readdir($directoryList)) {
                if ($file != '.' && $file != '..') {
                    $path = $directory . '/' . $file;
                    if (is_readable($path)) {
                        if (is_dir($path)) {
                            return scanDirectoryImages($path, $exts);
                        }
                        if (
                            is_file($path)
                            && in_array(end(explode('.', end(explode('/', $path)))), $exts)
                        ) {
                            $html .= '<a href="' . $path . '"><img src="' . $path
                                . '" style="max-height:100px;max-width:100px" /></a>';
                        }
                    }
                }
            }
            closedir($directoryList);
        }
        return $html;
    }
    
    0 讨论(0)
  • 2020-11-29 19:54

    You need to change the loop from for ($i=1; $i<count($files); $i++) to for ($i=0; $i<count($files); $i++):

    So the correct code is

    <?php
    $files = glob("images/*.*");
    
    for ($i=0; $i<count($files); $i++) {
        $image = $files[$i];
        print $image ."<br />";
        echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
    }
    
    ?>
    
    0 讨论(0)
  • 2020-11-29 19:54

    Strict Standards: Only variables should be passed by reference in /home/aadarshi/public_html/----------/upload/view.php on line 32

    and the code is:

    <?php
    
    echo scanDirectoryImages("uploads");
    
    /**
    * Recursively search through directory for images and display them
    * 
    * @param  array  $exts
    * @param  string $directory
    * @return string
    */
    function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
    {
    if (substr($directory, -1) == '/') {
        $directory = substr($directory, 0, -1);
    }
    $html = '';
    if (
        is_readable($directory)
        && (file_exists($directory) || is_dir($directory))
    ) {
        $directoryList = opendir($directory);
        while($file = readdir($directoryList)) {
            if ($file != '.' && $file != '..') {
                $path = $directory . '/' . $file;
                if (is_readable($path)) {
                    if (is_dir($path)) {
                        return scanDirectoryImages($path, $exts);
                    }
                    if (
                        is_file($path)
                        && in_array(end(explode('.', end(explode('/', $path)))),   $exts)
                    ) {
                        $html .= '<a href="' . $path . '"><img src="' . $path
                            . '" style="max-height:100px;max-width:100px" />  </a>';
                    }
                }
            }
        }
        closedir($directoryList);
    }
    return $html;
    }
    
    0 讨论(0)
  • 2020-11-29 20:14

    You can also use glob for this:

    $dirname = "media/images/iconized/";
    $images = glob($dirname."*.png");
    
    foreach($images as $image) {
        echo '<img src="'.$image.'" /><br />';
    }
    
    0 讨论(0)
  • 2020-11-29 20:15

    You can display all image from a folder using simple php script. Suppose folder name “images” and put some image in this folder and then use any text editor and paste this code and run this script. This is php code

        <?php
         $files = glob("images/*.*");
         for ($i=0; $i<count($files); $i++)
          {
            $image = $files[$i];
            $supported_file = array(
                    'gif',
                    'jpg',
                    'jpeg',
                    'png'
             );
    
             $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
             if (in_array($ext, $supported_file)) {
                echo basename($image)."<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
                 echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
                } else {
                    continue;
                }
              }
           ?>
    

    if you do not check image type then use this code

    <?php
    $files = glob("images/*.*");
    for ($i = 0; $i < count($files); $i++) {
        $image = $files[$i];
        echo basename($image) . "<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
        echo '<img src="' . $image . '" alt="Random image" />' . "<br /><br />";
    
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题