【问题标题】:How to display images from a folder using php - PHP如何使用 php - PHP 显示文件夹中的图像
【发布时间】:2021-12-20 17:31:42
【问题描述】:

如果有人能帮我弄清楚为什么浏览器无法加载图像(错误 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, '" />';
        }
    }
}

【问题讨论】:

  • 那你为什么不发布代码?
  • 刚刚做了。忘记缩进了。
  • 上面的代码有语法错误,比如 in_array( 没有右括号。回显中的 $ char
  • 有人需要学习基本语法
  • @user2837048 尝试使用相对路径而不是绝对路径。

标签: php


【解决方案1】:

您对以下陈述有误。利用 。不是,

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

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

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

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

【讨论】:

  • 其实对于echo语句可以用逗号代替concat操作符
  • 与 . 相比,逗号实际上具有更好的性能。关于回声语句
  • 字符串连接占用大量内存
  • @user2837048,您是否更正了代码中的语法错误?
  • 是的,我做到了,正如你所知道的那样。
【解决方案2】:

这是一个可能的解决方案,我的 cmets 上的解决方案 #3 对 brubill 的回答:

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

    方法 2. 不安全但更灵活。

    把图片放在任意目录下(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”语句并检查文件的结尾是否为“。”或“..”是不必要的,除非我错过了什么,然后想知道什么。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多