2011-09-29 165 views
0

我試圖創建一些代碼,以便用戶可以在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'); 

?> 

回答

3

你目前的問題是,當單引號,$file不會被解析,但字面上理解。爲什麼你要發送五個不同的內容類型

  • :使用雙或沒有引號:

    readfile($file); 
    

    其他意見?這沒有意義。您應該檢測文件的類型併發送相應的圖像標題(如果您只想提供可下載的資源,並且不嘗試在瀏覽器中顯示它),則發送相應的圖像標題(或者只是application/octet-stream)。

  • 您應該檢查$file是否指向您想允許下載的文件之一。如果沒有檢查,攻擊者可以通過這種方式從您的站點獲取任意文件,其中包括配置文件。

0

您無法多次定義內容類型。通過文件擴展名確定內容類型,幷包含正確的內容。

除此之外,使用此:

readfile($file); 

不需要引號。編輯:引用它甚至不工作,但它會嘗試打開一個名爲$文件的文件名

+0

好吧,當我改變這個ReadFile的($文件)使用;該頁面只是空白..still相同的圖像仍然損壞... :( – ruslyrossi

+0

文件是否存在你試圖打開它的位置? –

+0

是的..但圖像仍然損壞 – ruslyrossi

0

您發送多個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

0
 $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));