提高imageUpload的安全性,img src存在问题;image.php“;
似乎无法让php脚本执行和获取图像脚本,image placeholder.jpg在public_html webroot之外排序,下面是我要做的 显示image.php文件提高imageUpload的安全性,img src存在问题;image.php“;,php,Php,似乎无法让php脚本执行和获取图像脚本,image placeholder.jpg在public_html webroot之外排序,下面是我要做的 显示image.php文件 <img src="image.php?image=<?php echo urlencode('placeholder.jpg') ?>"/> “/> image.php <?php $file = basename(urldecode($_GET['image']));
<img src="image.php?image=<?php echo urlencode('placeholder.jpg') ?>"/>
“/>
image.php
<?php
$file = basename(urldecode($_GET['image']));
$fileDir = '/noaccess/avatars/';
if (file_exists($fileDir . $file))
{
$contents = file_get_contents($fileDir . $file);
header('Content-type: image/jpg');
echo $contents;
}
?>
运行这种脚本是非常不安全的,因为你让黑客访问你的文件系统。记住,他们可以使用
。/
浏览文件夹,
但无论如何,试试这个:
<?php
$file = basename(urldecode($_GET['image']));
$fileDir = $_SERVER['DOCUMENT_ROOT'] . 'noaccess/avatars/';
if (file_exists($fileDir . $file))
{
$imageRes = imagecreatefromjpeg($fileDir . $file);
header('Content-Type: image/jpeg');
// Output the image
@imagepng($imageRes);
// Free up memory
@imagedestroy($imageRes);
die();
}
?>
有人告诉我,这种方法更安全,更难执行文件,如果他们上传恶意的东西,从理论上讲,有些人只能通过/noaccess/avatars/
dir@OneTrickPony他们不能访问/noaccess/avatars/./../fileOutsideThoseFolders.txt
?我的sol的优势注意:如果他们试图访问的文件不是图像,imagecreatefromstring将失败并返回false,并且不会执行任何操作。\@OneTrickPony符号指向file\u get\u contents()
可以接受相对路径。警告读者:此代码使用的是file\u get\u contents()
,并且可以接受任意文件路径(包括一个相对文件路径)。file_get_contents。这样的脚本可以让黑客下载PHP脚本有读取权限的服务器上的任何文件,包括PHP源文件。