Home >Backend Development >PHP Tutorial >How to read pictures in a folder?

How to read pictures in a folder?

WBOY
WBOYOriginal
2016-08-18 09:16:251533browse

This can display a picture, but what should I do if I want to display all the pictures in the img folder? It doesn't work if I change it to $url = 'upload/'; So what should I do?

<code>header("Content-Type: image/jpeg;text/html; charset=utf-8");
$url='1.jpg';
$img = file_get_contents($url,true);
echo $img;</code>

Reply content:

This can display a picture, but what should I do if I want to display all the pictures in the img folder? It doesn't work if I change it to $url = 'upload/'; So what should I do?

<code>header("Content-Type: image/jpeg;text/html; charset=utf-8");
$url='1.jpg';
$img = file_get_contents($url,true);
echo $img;</code>

There are many methods, examples are as follows

<code>$dir = "upload/";  //要获取的目录
if (is_dir($dir)){
    if ($dh = opendir($dir)){
        while (($file = readdir($dh))!= false){
            $filePath = $dir.$file;
            echo "<img src='".$filePath."'/>";
        }
        closedir($dh);
    }
}</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn