i am trying to add images to zip file and forcing it with headers to download it. Below are the code snippet which i am using right now.
$zip = new ZipArchive();
$zip_name = 'design_images_'.time() .".zip";
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "* Sorry ZIP creation failed at this time";
}
$i = 0;
foreach($valid_files as $file){
$image_name = 'design_image_' . $i;
$zip->addFile($file, $image_name);
$i++;
}
$zip->close();
if(file_exists($zip_name)){
// force to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
}
When i try to open the zip file it gives error: the archive is corrupt.
Try to clean the output buffer before serving the download:
if(file_exists($zip_name)){
// Clean the output buffer.
while (ob_get_level()) {
ob_end_clean();
}
// force to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
}
Related
There's this code which I have
function pushfile(&$file) {
header_remove();
if (isset($file["type"])) header('Content-Type: '.$file["type"]);
if (isset($file["name"])) header('Content-Disposition: attachment; filename="'.$file["name"].'"');
print $file["name"];
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: ");
echo $file["content"];
exit;
$h=fopen($filename,"r");
$content = fread($h, filesize($filename));
fclose($h);
$file["type"] = "application/"; //
$file["name"] = $real_filename;
$file["content"] = $content."*".$context['user']['id'];
pushfile($file);
This ID number was well placed into the downloaded file, but it disappeared after converting it (e.g. from pdf to epub). Is there a good solution for that?
Problem with making zip file not able to create and download show only zip name in response
if (file_exists($filename)) {
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
$mimeType = 'application/json';
header("Content-type: " . $mimeType);
header("Content-Disposition: attachment; filename=\"{$filename}\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . mb_strlen($filename));
echo $filename;
flush();
readfile($filename);
// delete file
unlink($filename);
}
Try to set the Content-type like this: header("Content-type: application/zip");
<body>
<?php
$zip = new ZipArchive;
if ($zip->open(getcwd() .'/read.zip', ZipArchive::CREATE) === TRUE) {
$zip->addFile(getcwd() . '/read.txt','/newname.txt');
$zip->close();
$file = getcwd() . '/read.zip';
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"read.zip\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);
echo 'ok';
} else {
echo 'failed';
}
?>
</body>
I have following code and want to download zip file automatically that is being produced by this code when we run this page.
u need to add header for the client about file downloading. just see the manual example: http://php.net/manual/en/function.header.php#example-5315
<?php
$file = getcwd() . '/read.zip';
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"read.zip\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
ob_end_flush();
#readfile($file);
I tried to write a php-script to create and download a zip file. When I tested the script on my localhost, the download works, but when it's uploaded to the server, things go wrong: instead of downloading the zip file, the content of the file is displayed in the browser.
Can somebody point me in the right direction?
The code
$zip = new ZipArchive();
$zip->open("$maand/zipfile.zip", ZipArchive::OVERWRITE);
$zip->addFile("$maand/ant/a_nb.txt", 'ant.txt');
$zip->addFile("$maand/lim/l_nb.txt", 'lim.txt');
$zip->addFile("$maand/oos/o_nb.txt", 'oos.txt');
$zip->addFile("$maand/vla/v_nb.txt", 'vla.txt');
$zip->addFile("$maand/wes/w_nb.txt", 'wes.txt');
$zip->close();
$filename = "zipfile.zip";
$filepath = "$maand/";
// headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
you missing ob_start()
example:
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $download . '"');
ob_start();
$str = '';
if(file_exists($file) === true){
$str = file_get_contents($file);
}
ob_end_clean();
echo $str;
<?php
// force to download a file
if(isset($_POST['download'])){
$file = "images/dansyo_logo.png";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));
header( "Content-Description: File Transfer");
#readfile($file);
if(#readfile($file)){
echo'proceed';
}else{
echo'failded';
}
}
?>
<form method='post'>
<button name='download'>download</button>
</form>
I have the above code where i want the code to be able to download a file and in the same time insert values into my database.The code is functioning well that is i'm able to download a file.However,nothing is echoed after the file has downloaded or is downloading.
Its not going to echo since header will send the file to browser for download, if you wanted to do any kind of db submission just do it before header sent
$file = "images/dansyo_logo.png";
//Do your DB update here, before the header
if(is_readable($file)) {
// insert into database
} else {
exit(basename($file)." not found.");
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Description: File Transfer");
#readfile($file);
exit();
Note: file_exists() will return true on a directory.