Search code examples
phpzipphp-ziparchive

How can I download a Zip file from Localhost in php?


I am trying to download a zip file from my localhost.The file is downloading but gives an error message "invalid" when I open it. I am using the following code:-

 $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;  
    }`

Solution

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