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

10

希望有人能帮我找出浏览器为何无法加载图像(错误404)。代码是正确的,图像源也正确,但我无法找出问题所在。(使用本地主机)

$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, '" />';
        }
    }
}

那你为什么不发布代码呢? - Tepken Vannkorn
刚刚完成了,忘记缩进了。 - user2837048
上面的代码存在语法错误,例如in_array(没有闭合括号)。echo中的$符号也有问题。 - jerjer
4
需要有人学习基本语法。 - Jo E.
1
@user2837048 请尝试使用相对路径而不是绝对路径。 - Funk Forty Niner
显示剩余2条评论
3个回答

11

在下面的陈述中,您犯了一个错误。请使用“.”(点号),而不是“,”(逗号)。

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

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

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

3
实际上,对于echo语句,逗号可以用来代替连接运算符。 - jerjer
1
逗号在echo语句中的性能实际上比点号更好。 - jerjer
1
字符串拼接会占用大量内存。 - jerjer
@user2837048,你已经纠正了代码中的语法错误了吗? - jerjer
是的,我做到了,正如你所看到的。 - user2837048
显示剩余6条评论

9
以下是我的评论中对blubill答案的解决方案#3的可能解决方案:
yourscript.php
========================
<?php
    $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)     
            {
                $name = basename($file);
                echo "<img src='img.php?name={$name}' />";
            }
        }
    }
?>


img.php
========================
<?php
    $name = $_GET['name'];
    $mimes = array
    (
        'jpg' => 'image/jpg',
        'jpeg' => 'image/jpg',
        'gif' => 'image/gif',
        'png' => 'image/png'
    );

    $ext = strtolower(end(explode('.', $name)));

    $file = '/home/users/Pictures/'.$name;
    header('content-type: '. $mimes[$ext]);
    header('content-disposition: inline; filename="'.$name.'";');
    readfile($file);
?>

3
你有两种方法可以实现这个目标:
方法1. 安全的方式。
将图片放在 /www/htdocs/ 目录下。
<?php
    $www_root = 'http://localhost/images';
    $dir = '/var/www/images';
    $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)) ) {
              echo '<img src="', $www_root, '/', $file, '" alt="', $file, '"/>';
           break;
           }
        }
    }
?>

方法二:不安全但更加灵活。
将图片放在任意目录中(Apache 必须具有读取文件的权限)。
<?php
    $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)) ) {
              echo '<img src="file_viewer.php?file=', base64_encode($dir . '/' . $file), '" alt="', $file, '"/>';
           break;
           }
        }
    }
?>

并创建另一个脚本来读取图像文件。
<?php
    $filename = base64_decode($_GET['file']);
    // Check the folder location to avoid exploit
    if (dirname($filename) == '/home/user/Pictures')
        echo file_get_contents($filename);
?>

考虑到第一种方法,我认为“break”语句以及检查文件结尾是否为“。”或“..”是不必要的,除非我漏掉了什么,那就请告诉我。 - Darius Miliauskas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接