Wednesday, 15 August 2012

PHP downloading .zip file but incorrectly -



PHP downloading .zip file but incorrectly -

i trying download zip file local server on hd.

$zip = new ziparchive; $zip->open($archive, ziparchive::create); $files = scandir($dir); unset($files[0], $files[1]); foreach ($files $file) { $zip->addfile($dir.'/'.$file); } $zip->close(); $download=$archive; header("content-disposition: attachment; filename=\"reports". $stocktake_id. ".zip"); header("pragma: public"); header("cache-control: must-revalidate, post-check=0, pre-check=0");

the file gets downloaded size of wrong , doesn't open properly, saying format either invalid or file corrupted.

i think missing content-type header. also, don't see echo'ing raw info clientside? and, create sure there not debug output or not in file itself.

//after saved file: download_file_to_client($saved_file); exit; function download_file_to_client($file, $mime = 'application/zip') { $filesize = filesize($file); header('content-type: '.$mime.';charset=utf-8'); header('content-disposition: attachment;filename="some-filename.zip"'); header('pragma: no-cache'); header('expires: 0'); header('content-length: ' . $filesize); flush(); // headers out prevent delay in browsers. $fh = fopen($file, "r"); while ($fh && !feof($fh)) { echo fread($fh, 8192); } fclose($fh); }

php

No comments:

Post a Comment