php 访问位于服务器根目录之前/之外的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13357994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access a file which is located before / outside the server root directory?
提问by Weacked
I'm making an intranet for a post-sale customer service entreprise. Employee need to be able to upload img files to the intranet's server and i need to store them in a directory with is BEFORE www(the website's root directory).
我正在为售后客户服务企业制作内联网。员工需要能够将 img 文件上传到 Intranet 的服务器,我需要将它们存储在一个目录中,is BEFORE www(网站的根目录)。
Doing this using php is pretty easy but how to include these imgs on the website once they're uploaded ? I tried this code
使用 php 执行此操作非常简单,但是如何在上传这些 img 后将其包含在网站上?我试过这个代码
<img src="../img/img.png"/>
This is not working because i can't send a file if it is OUTSIDE the server's wwwdirectory ...
这是行不通的,因为如果文件在服务器www目录之外,我将无法发送文件...
Is there any proper way to do that ?
有什么正确的方法可以做到这一点吗?
Current treeview :
当前树视图:
server root directory
|www
|(all server files)
|img
|(all img files)
(the server's index.php is located in wwwand the files are in img)
(服务器的 index.php 位于,www文件位于img)
回答by Briareos386
You cannot directly access any file outside your web directory. As your question includes the tag PHP as well, I assume you may want to use it.
您无法直接访问 Web 目录之外的任何文件。由于您的问题也包括 PHP 标签,我假设您可能想要使用它。
What you can do is the following:
您可以执行以下操作:
Inside your www directory, create a "image.php" file, with a similar content to:
在您的 www 目录中,创建一个“image.php”文件,其内容类似于:
<?php
header('Content-Type: image/png');
readfile("../img/" . $_GET['img']);
?>
And call your images with
并调用您的图像
<img src="image.php?img=myimage.png" />
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.
请注意,您的 PHP 文件不应该那么简单:) 因为您可能想要处理多种图像格式(并为它们提供正确的标题),检查恶意文件路径/包含(您不想使用 $ _GET 而不验证/清理输入),额外的缓存等等等等。
But this should give you an idea on how you can target your issue.
但这应该让您了解如何定位您的问题。
回答by Thomas
It depends on what you are trying to accomplish and how. With "simple" html commands it is as you found out. You can't go to a directory outside of the www root. (for xampp applications on C for exmple it is most of the time c:\xampp\htdocs).
这取决于您要完成什么以及如何完成。使用“简单”的 html 命令,正如您所发现的那样。您不能转到 www 根目录之外的目录。(例如,对于 C 上的 xampp 应用程序,大部分时间是 c:\xampp\htdocs)。
If you are ok with using serverside commands to accomplish what you want to achieve then you could use php to do a workaround, by reading the appropriate files in via PHP. For example if your file is named "myimg.gif" and lies in "c:\pics"
如果您可以使用服务器端命令来完成您想要实现的目标,那么您可以使用 php 来做一个解决方法,通过 PHP 读取适当的文件。例如,如果您的文件名为“myimg.gif”并且位于“c:\pics”
<img SRC="data:image/gif;base64,<?php echo base64_encode(file_get_contents("c:\pics\myimg.gif"));?>">
With that you are reading the contents of the file and writing it directly into the img tag. Be aware that you need to change image/gif to what you really need there.
这样您就可以读取文件的内容并将其直接写入 img 标签。请注意,您需要将图像/gif 更改为您真正需要的内容。
回答by Kamil
You can't directly access file outside/above your root directory (www or public_html).
您不能直接访问根目录(www 或 public_html)之外/之上的文件。
You can create virtual directory in Apache server configuration. Google "apache virtual directory" for more information.
您可以在 Apache 服务器配置中创建虚拟目录。谷歌“apache 虚拟目录”了解更多信息。
Virtual directory configuration example:
虚拟目录配置示例:
<IfModule alias_module>
Alias /uploaded_images/ "/home/someuser/somewhere"
<Directory "/home/someuser/somewhere">
Allow from all
</Directory>
</IfModule>
uploaded_imagesdirectory will be available from web like normal directory in www directory. You can copy files from there to another location (kind of buffering).
uploaded_images目录将像 www 目录中的普通目录一样从网络上可用。您可以将文件从那里复制到另一个位置(一种缓冲)。
You can also open/copy that file by ftp from php level without changing anything in apache, but this may be really slow (use it only if you can't control apache config, virtual directory is much better).
您还可以从 php 级别通过 ftp 打开/复制该文件,而无需更改 apache 中的任何内容,但这可能非常慢(仅当您无法控制 apache 配置时才使用它,虚拟目录要好得多)。
回答by Jefri P.
I Agree with @kamil's idea about creating a virtual directory. However if you want to go the php route, I wrote some code that open's images in a directory before WWW and copies them to the www/images folder.
我同意@kamil 关于创建虚拟目录的想法。但是,如果你想走 php 路线,我写了一些代码,在 WWW 之前的目录中打开图像并将它们复制到 www/images 文件夹。
<?php
$imagepath = "../images/";
define('IMGPATH', realpath($imagepath).DIRECTORY_SEPARATOR);
$cachewwwimg = "images/";
$imagename = 'image.jpg';
copy(IMGPATH.$imagename, $cachewwwimg.$imagename)
?>
<img src="<?php echo $cacheimg.$imagename;?>"/>

