gpt4 book ai didi

php - 用php保存图像文件?

转载 作者:行者123 更新时间:2023-12-01 18:52:27 24 4
gpt4 key购买 nike

我的根目录中有此代码,可以在用户单击链接时自动保存图像文件

<?
// Force download of image file specified in URL query string and which
// is in the same directory as this script:
if(!empty($_GET['file']))
{
$filename = $_GET['file']; // don't accept other directories
$size = @getimagesize($filename);
$fp = @fopen($filename, "rb");
if ($size && $fp)
{
header("Content-type: {$size['mime']}");
header("Content-Length: " . filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
fpassthru($fp);
exit;
}
}
header("HTTP/1.0 404 Not Found");
?>

问题:

  1. 黑客能否将任何字符串发送到此文件并保存我的根文件? (例如:index.php 等...)

  2. 如何检查 URL 是否包含图像文件?

最佳答案

问题 1:您应该限制对特定目录的访问 - 如果这只是 1 个目录,那么您无需使用 $_GET 请求提供的目录,只需将其包含在您的目录中即可改为 php。

$filename = end( explode( '/', $_GET['file'] ) );
$filename = '/uploadsdirectory/' . $filename;

问题2:要检查所请求文件的扩展名,您应该执行以下操作:

$extension = end( explode( '.', $filename ) );

这将返回jpgpng等..基本上是最后一个时期之后的内容

关于php - 用php保存图像文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25425687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com