如何在php中从Localhost下载Zip文件?

时间:2013-11-21 18:45:27

标签: php zip ziparchive

我正在尝试从我的localhost下载一个zip文件。该文件正在下载,但在我打开它时会显示错误消息“无效”。 我使用以下代码: -

 $filename = "markers.zip";  
`if(file_exists($filename) && is_readable($filename)){  
        header("Content-Disposition: attachment; filename=".basename($filename));  
        header("Content-Type: application/force-download");  
        header("Content-Type: application/zip");  
        header("Content-Description: File Transfer");  
        header("Content-Length: " . filesize($filename));  
        flush();  
        $fp = fopen($filename, "r");  
        while (!feof($fp))  
        {  
            echo fread($fp, 65536);  
            flush();  
        }  
        fclose($fp);  
        exit;  
    }`

1 个答案:

答案 0 :(得分:1)

<?php
$file = "file.zip";
header('Content-type: application/x-download');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Length: '.filesize($file));
readfile($file);
?>