如何使用php显示文件夹中的图像-PHP [英] How to display images from a folder using php - PHP

查看:69
本文介绍了如何使用php显示文件夹中的图像-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以帮助我弄清为什么浏览器无法加载图像(错误404),那就太好了.该代码有效,并且图像源是正确的,但是我无法弄清楚出了什么问题. (使用localhost)

It would be great if someone could help me figure out why the browser cannot load the images (error 404). The code works, and the image source is correct, but I cannot figure out what is wrong. (using 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, '" />';
        }
    }
}

推荐答案

您在以下声明中有误.使用 .不是,

You had a mistake on the statement below. Use . not ,

echo '<img src="', $dir, '/', $file, '" alt="', $file, $

echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. $

echo 'Directory \'', $dir, '\' not found!';

echo 'Directory \''. $dir. '\' not found!';

这篇关于如何使用php显示文件夹中的图像-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆