php 从指定目录中拉取所有图像,然后显示它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11903289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:24:53  来源:igfitidea点击:

Pull all images from a specified directory and then display them

php

提问by Juliver Galleto

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.

如何显示指定目录中的图像?就像我想显示目录中的所有 png 图像一样,在我的情况下,我的目录是 media/images/iconized。

I tried to look around but seems none of them fits what i really needed.

我试着环顾四周,但似乎没有一个适合我真正需要的。

But here's my try.

但这是我的尝试。

$dirname = "media/images/iconized/";
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='media/images/iconized/$curimg' /><br>\n";
};
}

hope someone here could help. Im open in any ideas, recommendation and suggestion, thank you.

希望这里有人可以提供帮助。我对任何想法,推荐和建议持开放态度,谢谢。

回答by Loken Makwana

You can also use globfor this:

您也可以glob为此使用:

$dirname = "media/images/iconized/";
$images = glob($dirname."*.png");

foreach($images as $image) {
    echo '<img src="'.$image.'" /><br />';
}

回答by Shafiqul Islam

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

您可以使用简单的 php 脚本显示文件夹中的所有图像。假设文件夹名为“images”并在此文件夹中放置一些图像,然后您使用编辑器粘贴此代码并运行此脚本。这是php代码

    <?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 />";

}
?>

回答by Belal Almassri

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

您需要将循环从 更改for ($i=1; $i<count($files); $i++)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 />";
}

?>

回答by Steve Robbins

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;
}

回答by AAKASH HACKER

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

严格标准:在第 32 行的 /home/aadarshi/public_html/---------/upload/view.php 中只应通过引用传递变量

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;
}