相关疑难解决方法(0)

如何使用PHP - PHP显示文件夹中的图像

如果有人可以帮我弄清楚为什么浏览器无法加载图像会很棒(错误404).代码工作,图像源是正确的,但我无法弄清楚出了什么问题.(使用localhost)

$dir          = '/home/user/Pictures';
$file_display = array(
    'jpg',
    'jpeg',
    'png',
    'gif'
);

if (file_exists($dir) == false) {
    echo 'Directory \'', $dir, '\' not found!';
} else {
    $dir_contents = scandir($dir);

    foreach ($dir_contents as $file) {
        $file_type = strtolower(end(explode('.', $file)));

        if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
            echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

php

10
推荐指数
2
解决办法
14万
查看次数

标签 统计

php ×1