2014-09-13 177 views
-1

我有一個文件image.php,動態查看Zip文件夾內的圖像。 因此image.php?location =。/ folder/Hi.zip & file = image.jpg查看圖像。強制php下載ZIP中的文件

我有另一個文件downloadfile.php強制下載參數中指定的文件。 downloadfile.php?位置= .folder/&文件= image.jpg的*下載圖像*

我需要的是使用downloadfile.php下載動態生成圖像(由image.php)。

downloadfile.php

<?php 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
$file=$_GET['file'];$ext=$_GET['ext'];$location=$_GET['location']; 
header('Content-disposition: attachment; filename="'.$file.'"'); 
header('Content-type:"'.$ext.'"'); 
readfile("$location"."$file"); 
?> 

image.php

<?php 
function showimage($zip_file,$file_name) 
{ 
    $z=new ZipArchive(); 
    if($z->open($zip_file)!=true){ 
     echo "File not found"; 
     return false; 
    } 
    $stat=$z->statName($file_name); 

    $fp=$z->getStream($file_name); 
    if(!$fp){ 
     echo "Could not load image"; 
     return false; 
    } 
    header('Content-Type:image/'.$_GET['type']); 
    header('Content-Length:'. $stat['size']); 
    fpassthru($fp); 
    return true; 
} 
showimage($_GET['zip'],$_GET['file']); 
?> 

任何建議?

回答

2

爲了迫使瀏覽器下載一個圖像文件,你可以從image.php 使用的代碼,只是從

header('Content-Type:image/'.$_GET['type']); 
header('Content-Length:'. $stat['size']); 

改變標題 到

header("Content-Transfer-Encoding: binary"); 
header('Content-Description: File Transfer'); 
header('Content-Type:image/'.$_GET['type']); 
header('Content-Length: ' . $stat['size']); 
header('Content-Disposition: attachment; filename=' . $file_name); 

這將迫使用戶下載而不是顯示圖像。