用PHP从外部public_html读取图像? [英] Reading image from outside public_html with PHP?

查看:126
本文介绍了用PHP从外部public_html读取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的很愚蠢,但是我一直在尝试几十种不同的东西,我无法为我的生活做出错误的处理。



我有一个cpanel安装,与通常的public_html目录和访问下面的目录(调用该目录USER)。我正在用图像测试一些东西。



我已经将一个图像文件放在public_html上方的USER目录中。


$ b $我已经写了一个测试php脚本,只有几行:

  if(is_file(../ 1.jpg)){
echo< img src = \../ 1.jpg\/>;
} else {
echonot there;
}

is_file可以随时找到该文件。还尝试过file_exists和is_readable,这些都会找到该文件。



但即使脚本看到该文件,我无法解决如何将该文件输出到浏览器。或者即使可以这样做。



我将文件的权限更改为全部可读。



我试过从根目录放置绝对路径。



我尝试使用imagejpeg。



这些东西都不行。



你可能会想知道为什么我不要把图像放在public_html ,但是这个脚本是我正在做的测试,我需要弄清楚在同一台服务器上提供其他域/帐户的图像(它是一个专用的服务器)可能的可能性。



我可以理解,也许这是一个权限的事情,在这种情况下,我不明白为什么脚本可以找到文件 - 无论如何,我可能忽略了一些非常愚蠢或基本的东西。所以如果有人能指出我正确的方向,我会很感激。



非常感谢

解决方案

这是无法做到的。当您输出一个img标签时,这只是服务器上的一些文本。它被发送到浏览器,浏览器是试图获取 src =...部分中列出的文件。这是通过HTTP请求完成的,所以如果图像在文档根目录之外,您不能直接通过定义获取文件。



如果可以简单地执行 src =../../../../ picture.jpg并获取文档根目录以外的图像,您可以在服务器上获取任何文件,包括配置文件,你的密码文件,blah blah blah。这将是一个足够大的安全漏洞,可以使其成为疯狂的资格。



如果您想要提供对文档根目录以外的文件的访问,则必须执行它通过一个脚本。最简单的方法是:



image.php:

  <?php 
header('Content-type:image / jpeg');
readfile('/ path / to / picture / outside / doc / root / file.jpg');

和您的html:

 < img src =image.php> 

还有其他nastier选项,例如使用Apache alias指令将外部目录映射到内部重写网址等等,但这只会使事情变得更加复杂。


this is really stupid, but I been trying dozens of different things and I can't for the life of me work out what I am doing wrong.

I have a cpanel installation, with the usual public_html directory and access to the directory below that (call that directory "USER"). I am testing out some things with images.

I have put an image file above the public_html, into the USER directory.

I have written a test php script which has just a few lines:

if(is_file("../1.jpg")){    
    echo "<img src=\"../1.jpg\" />";
}else{
    echo "not there";
}

The is_file can always find the file. Also tried file_exists and is_readable, these all find the file too.

But even though the script sees the file, I can't work out how to output that file to the browser. Or even if it is possible to do it this way.

I've changed the permissions on the file to all readable.

I've tried putting an absolute path from the root.

I tried using imagejpeg.

None of these things work.

You may wonder why I don't just put the image within public_html, but this script is a test I'm doing coz I need to figure out what is possible when it comes to serving images from other domains / accounts on the same server (it is a dedicated server).

I can understand that maybe it's a permissions thing or something - in which case I don't understand why the script can find the file - well anyway I am probably overlooking something very stupid or basic. So if anyone could point me in the right direction I would appreciate it.

Many thanks

解决方案

That cannot be done. When you output an img tag, that's simply some text on the server. It's sent to the browser and the browser is what attempts to fetch the file listed in the src="..." portion. That's done via an HTTP request, so if the image is outside of the document root, you cannot by definition fetch the file directly.

If you could simply do src="../../../../picture.jpg" and get an image outside of the document root, you could fetch ANY file on the server, including your config files, your password files, blah blah blah. It'd be a security hole big enough to qualify as insane.

if you want to provide access to a file outside of the document root, you'll have to do it via a script. The simplest method is to have:

image.php:

<?php
header('Content-type: image/jpeg');
readfile('/path/to/picture/outside/doc/root/file.jpg');

and in your html:

<img src="image.php">

There's other nastier options, such as using Apache alias directives to map external directories into "internal" ones, rewriting urls, etc... but that just makes things even more complicated.

这篇关于用PHP从外部public_html读取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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