2014-09-13 182 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); 

这将迫使用户下载而不是显示图像。