我尝试创建一些代码,这样用户就可以在opencart中下载图像,而不需要右键单击和使用Save as...,但是在我创建了这段代码并下载了图像之后,试图打开图像显示图像是损坏的。
<form action="download.php" method="post" >
<input type="text" name="file" value="<?php echo $image['popup']; ?>" />
<input type="submit" value="download" />
</form>download.php
<?php
$file= $_POST["file"];
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header ("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-type: application/pdf');
header('Content-type: image/jpg');
header('Content-type: image/gif');
header('Content-type: image/png');
readfile('$file');
?>发布于 2011-09-29 08:09:40
您当前的问题是,当在单引号中时,$file将不会被解析,而是字面意思。使用双引号或无引号:
readfile($file);其他意见:
application/octet-stream,而不是尝试在浏览器中显示它)。$file实际上指向要允许下载的文件之一。如果没有这种检查,攻击者就可以以这种方式从您的站点获取任何文件,包括配置文件.。
发布于 2011-09-29 08:10:17
您不可能多次定义内容类型。根据文件扩展名确定内容类型,并包括正确的内容类型。
除此之外,请使用这个:
readfile($file);不需要引用。编辑:使用引号,它甚至不能工作,但它将尝试打开一个名为$file的文件名
发布于 2011-09-29 08:10:29
你发送了多个内容类型的标题?这是错误的。
您必须通过扩展来确定内容类型(.jpg,.pdf,.),然后发送适当的标头。
下面是我最近做的一个下载脚本的片段。
$fileName = $_POST['file']; // or whatever
$file = 'someDirectory/' . $fileName; // full path of the file, could also be absolute
$fileLen = filesize($file);
$ext = strtolower(substr(strrchr($fileName, '.'), 1));
switch($ext) {
case 'txt':
$cType = 'text/plain';
break;
case 'pdf':
$cType = 'application/pdf';
break;
case 'exe':
$cType = 'application/octet-stream';
break;
case 'zip':
$cType = 'application/zip';
break;
case 'doc':
$cType = 'application/msword';
break;
case 'xls':
$cType = 'application/vnd.ms-excel';
break;
case 'ppt':
$cType = 'application/vnd.ms-powerpoint';
break;
case 'gif':
$cType = 'image/gif';
break;
case 'png':
$cType = 'image/png';
break;
case 'jpeg':
case 'jpg':
$cType = 'image/jpg';
break;
case 'mp3':
$cType = 'audio/mpeg';
break;
case 'wav':
$cType = 'audio/x-wav';
break;
case 'mpeg':
case 'mpg':
case 'mpe':
$cType = 'video/mpeg';
break;
case 'mov':
$cType = 'video/quicktime';
break;
case 'avi':
$cType = 'video/x-msvideo';
break;
//forbidden filetypes
case 'inc':
case 'conf':
case 'sql':
case 'cgi':
case 'htaccess':
case 'php':
case 'php3':
case 'php4':
case 'php5':
exit;
default:
$cType = 'application/force-download';
break;
}
$headers = array(
'Pragma' => 'public',
'Expires' => 0,
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Cache-Control' => 'public',
'Content-Description' => 'File Transfer',
'Content-Type' => $cType,
'Content-Disposition' => 'attachment; filename="'. $fileName .'"',
'Content-Transfer-Encoding' => 'binary',
'Content-Length' => $fileLen
);
foreach($headers as $header => $data)
header($header . ': ' . $data);
@readfile($file); https://stackoverflow.com/questions/7594255
复制相似问题