从doc root移动图像以增加安全性

时间:2011-12-08 08:23:48

标签: php apache

我的网站使用bookmarklet从外部网站收集数据,有点像Pinterest。我担心安全问题,并希望将bookmarklet从doc根目录收集到的图像移动到一个级别。我的脚本有一些重要的安全检查,但我想将其添加为最后一道防线。

如何在脚本中访问我的图片?显然使用../userimages/id/image.jpg不会工作。我正在使用Apache。

谢谢!

1 个答案:

答案 0 :(得分:0)

代理图像

您可以使用代理脚本来提供图像,如下例所示:

// open the file in a binary mode
$name = '../userimages/id/image.jpg';
$fp = fopen($name, 'rb');

// send the right headers
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));

// you  may like to set some cache headers here

// dump the picture and stop the script
fpassthru($fp);
exit;

此示例来自PHP manuals fpassthru() page。您可以将此脚本保存在服务器文档root / httpdocs文件夹中的某个位置。

“欺骗”图像的URL

将PHP文件作为图像文件呈现给用户/浏览器的最简单方法是使用Apaches mod_rewrite。通常我使用这样的URL结构:

http://www.example.org/image-id/image.png

其中image-id是该特定图像的唯一标识符。这样,文件具有图像的正确扩展名,而不是.php