背景
执行此操作的规则:
domain.com/image.png
时使用它。就像我在下面提到的图片示例一样。我尝试了什么:
根据上面提到的当前问题和其他教程,我想出了 这样:
<?php
$location = dirname($_SERVER['DOCUMENT_ROOT']);
$image = $location . '/public_html/images/banned.png';
header('Content-Type:image/png');
header('Content-Length: ' . filesize($image));
echo file_get_contents($image);
?>
<img src=" <? echo $image; ?> "/>
它工作正常,这是一个例子:
然而,这不是我正在寻找或尝试做的事情。同样,我试图将其视为正常的图像src,因为它将用于个人资料图片或其他用途。
任何帮助将不胜感激。提前谢谢!
答案 0 :(得分:1)
此:
echo file_get_contents($image);
?>
<img src=" <? echo $image; ?> "/>
出于几个原因是错误的。首先,您不能以这种方式将原始图像内容与HTML标记混合。此外,src
指定图像的URL而不是原始内容。
您可以将echo file_get_contents($image);
及相关代码移动到单独的文件,即image.php
,然后在src
中引用它,将图像名称作为参数传递(即{{1} }) - 请注意,如果您这样做错了,您将对directory traversal attack开放。或者,您可以尝试将rectory traversal attack data URI scheme用作image.php?img=foo.jpg
参数,并以这种方式直接传递原始内容。
答案 1 :(得分:0)
将image.php更改为
<?php
function image() {
$location = dirname($_SERVER['DOCUMENT_ROOT']);
$image = $location . '/public_html/images/banned.png';
return base64_encode(file_get_contents($image));
}
?>
在另一个要显示该图片的文件中,让我们说test.php:
<?php
include("image.php");
?>
<img src='data:image/png;base64,<?= image(); ?>' >