允许PHP显示本地图像文件

时间:2015-07-10 14:40:37

标签: php

我想在Windows机器中显示存储在本地文件系统中的图像。

档案路径: D:/farewell/new/imagename.JPG

<html>
    <head>
        <title>
            Images Gallery
        </title>
    </head>
    <body>
        <?php
        $img="D:/farewell/new/IMG_0603.JPG";
        echo "<img src='$img' width=\"300\"/>";
        ?>
    </body>
</html>

网页未加载任何图片,但 Inspect Element 显示以下错误:

不允许加载本地资源:file:/// D:/farewell/new/IMG_0603.JPG images.php:10

如何允许PHP或Apache网络服务器或Chrome浏览器(无论情况如何)允许访问存储在其他分区中的图像?

编辑

我想访问D:/告别/新/中的所有图像。我尝试在 httpd.conf 下设置相同的别名,但这没用,因为无法将url提供给标记。

Alias /farewell/ "D:/farewell/2/"
<Directory "D:/farewell/2">
   Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

1 个答案:

答案 0 :(得分:1)

这是可能的 - 这是Apache 2.4 Windows版本。注意:要求所有已授予的,如下所述:Alias 403 Forbidden with Apache

Alias /farewell "D:/farewell/2/"
<Directory "D:/farewell/2/">
    Options Indexes FollowSymLinks MultiViews ExecCGI
    AllowOverride all 
    Require all granted
</Directory>

......那应该做到;你应该能够访问具有以下效果的图像:

<img src="/farewell/[YOUR_IMAGE]" ... />

要显示文件夹中的所有图像,您可以执行以下操作:

$aF = scandir("D:/farewell/2");

foreach($aF as $file) {
  if(preg_match('/\.jpg|\.gif|\.png/', $file)) {
    echo "<img src=\"/farewell/{$file}\" alt=\"{$file}\" />\n";
  }
}

从技术上讲,你 通过HTTP显示图像,但是你正在使用scandir()从文件系统中检索图像文件名 - 这在功能上几乎就是你所追求的。 / p>

简单地执行<img src=\"file:///D:/farewell/2/{$file}\" ... />将无法在任何现代浏览器中使用(据我所知)。

glob()版本(使用我的Steam屏幕截图文件夹测试)

foreach(glob("D:/farewell/2/*.{jpg,gif,png}", GLOB_BRACE) as $file) {
  $sFile = strrchr($file, "/");
  echo "<div><img src=\"/farewell{$sFile}\" alt=\"{$sFile}\" /></div>\n";
}
相关问题