我怎样才能显示在我的PHP应用程序的Web根目录以外的图像? [英] How can i show the images outside the web root directory in my php application?

查看:437
本文介绍了我怎样才能显示在我的PHP应用程序的Web根目录以外的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP Web应用程序的Apache服务器,Linux操作系统。对于某些安全原因,我保留网站根目录外的文件和图像。我该怎么让这些图片,当用户登录。

I have web application in PHP using apache server, linux. For some security reason i am keeping the documents and images outside web root. How can i show these images, when the user login.

推荐答案

PHP在默认情况下已经可以访问文件的Web根目录之外,除非有一个的open_basedir 指令限制(或安全模式,而是希望你在笼子里不是)。

PHP by default can already access files outside the web root, unless restricted with an open_basedir directive (or safe mode, but hope you're not in that cage).

这通常是一个很好的做法,一个虚拟主机配置中的的open_basedir 限制中插入。您可以指定由分隔的多个目录:在Linux和 ;在Windows上

It's normally a good practice to insert within a VirtualHost configuration an open_basedir restriction. You can specify multiple directories separated by : on Linux and ; on windows.

php_admin_value open_basedir /var/www/s/stage:/usr/share/php:/your/dir

要访问这些文件,或者使用绝对路径或相对于所谓的PHP文件的位置的路径。 (所以你必须 ../ 来达到上述级别)。

To access those files either use an absolute path or a path relative to the position of the PHP file called. (So you'll have to ../ to reach levels above).

另外要确保目录要在其中写入分配给网络服务器和用户有写权限。

Also be sure that directories in which you want to write to are assigned to the webserver user and have write permission.

,否则你有第二个选项:

在你的www目录,创建image.php的文件,具有相似的内容:

Inside your www directory, create a "image.php" file, with a similar content to:

<?php
  header('Content-Type: image/png');
  readfile("../img/" . $_GET['img']);
?>

和与打电话给你的图片

<img src="image.php?img=myimage.png" />

请注意,你的PHP文件不应该是如此简单:)至于你可能要应对多种图像格式(并为他们提供正确的标题),检查恶意文件路径/夹杂物(你不想使用$ _GET而无需验证/消毒输入),额外的缓存等等,等等等等。

Please be aware that your PHP file shouldn't be that simple :) As you may want to address multiple image formats (and providing the correct header for them), checking for malicious file path/inclusions (you don't want to use $_GET without validating/sanitizing the input), extra caching etc. etc. etc.

但是,这应该给你你如何能针对您的问题的想法。

But this should give you an idea on how you can target your issue.

这篇关于我怎样才能显示在我的PHP应用程序的Web根目录以外的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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