【问题标题】:How to echo image that is outside of the public folder如何回显公用文件夹之外的图像
【发布时间】:2015-02-08 17:45:25
【问题描述】:

在这个项目中,我将管理员提交的文件上传到公共文件夹之外的文件夹中。

/web/ (the public folder)
/upload/ (uploading image to this which is at the same level as the public folder)

现在虽然我可以使用绝对 url (/www/userid/upload/) 上传,但我无法使用相对或绝对 url 回显相同的图像。

我尝试过<img src="/www/userid/upload/photo.jpg" /><img src="../upload/photo.jpg" />,但没有一个有效。有什么提示吗?

【问题讨论】:

标签: php path directory server


【解决方案1】:

direct 路径不起作用,因为您无法从根文件夹外部获取内容.. 您可以通过另一种方式进行操作,从服务器目录路径读取 php 脚本中的图像文件并使用 header 提供文件/p>

脚本file.php

<?php
  $mime_type = mime_content_type("/image_path/{$_GET['file']}");
  header('Content-Type: '.$mime_type);

  readfile("/image_path/{$_GET['file']}");
?>


<img src="file.php?file=photo.jpg" />

【讨论】:

  • 谢谢@Girish。如果你想拥有多种图像类型,如 jpg、jpeg、png、x-png、gif 等,你会怎么做?
【解决方案2】:

您通常不会访问 Web 根目录之外的目录。

一个快速的解决方案是在 apache 配置中添加一个 alias 就像

alias /upload/ /www/userid/upload/

virtualhosts里面

并使用

/upload/photo.jpg

【讨论】:

    【解决方案3】:

    我会跳过 php 和文件夹选项并直接在服务器中使用符号链接。我假设您使用某种形式的 linux 作为您的服务器。因此,如果这不是安全问题,只需将上传文件夹复制为您拥有图像的公用文件夹即可。但是,这将使该文件夹中的所有内容都出现在上传文件夹中,但这不应该是很多问题,因为替代方法是更改​​上传文件夹的权限并以这种方式编写整个内容。

    【讨论】:

      猜你喜欢
      • 2020-05-22
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2014-02-03
      • 1970-01-01
      相关资源
      最近更新 更多