图像下载 - 图像无法打开

问题描述:

我试图创建一些代码,以便用户可以在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'); 

?> 

你目前的问题是,当单引号,$file不会被解析,但字面上理解。为什么你要发送五个不同的内容类型

  • :使用双或没有引号:

    readfile($file); 
    

    其他意见?这没有意义。您应该检测文件的类型并发送相应的图像标题(如果您只想提供可下载的资源,并且不尝试在浏览器中显示它),则发送相应的图像标题(或者只是application/octet-stream)。

  • 您应该检查$file是否指向您想允许下载的文件之一。如果没有检查,攻击者可以通过这种方式从您的站点获取任意文件,其中包括配置文件。

您无法多次定义内容类型。通过文件扩展名确定内容类型,并包含正确的内容。

除此之外,使用此:

readfile($file); 

不需要引号。编辑:引用它甚至不工作,但它会尝试打开一个名为$文件的文件名

+0

好吧,当我改变这个ReadFile的($文件)使用;该页面只是空白..still相同的图像仍然损坏... :( – ruslyrossi

+0

文件是否存在你试图打开它的位置? –

+0

是的..但图像仍然损坏 – ruslyrossi

您发送多个Content-Type标头?这是错误的。

您必须通过文件的扩展名(.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); 
+0

对不起..我;新手...所以如何修复我的代码..所以用户可以下载图像只有jpeg – ruslyrossi

+0

消息错误:损坏的内容错误;您尝试查看的页面无法显示,因为数据传输中的错误被检测到。 – ruslyrossi

 $mimeInfo = finfo_open(FILEINFO_MIME_TYPE); 
     $mime_type = finfo_file($mimeInfo, $_FILES['files']['tmp_name']); 

,这将帮助你检测一下MIME类型与

 header('Content-Type: '.$this->_mime_type); 
     header('Content-Disposition: attachment; filename='.basename($file)); 
     header('Content-Transfer-Encoding: binary'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate'); 
     header('Pragma: public'); 
     header('Content-Length: ' . filesize($file));