cant download zip file from php function - php

am calling this function to create and download zip. the create is working but my screen freezes when the system is about to download it, then shows a bunch of abstract writings without download
function zipF(){
$track = $_POST['track'];
$cartid = $_POST['cartid'];
$zip = new ZipArchive;
if ($zip->open('myzip.zip', ZipArchive::CREATE)) {
$notan = DataDB::getInstance()->select_from_where('order_image','cartid',$cartid);
foreach($notan as $row){
$zip->addFile('../img/tempfil/'.$row['image'], $row['image']);
}
$zip->close();
$archive_file_name ="myzip.zip";
header("Content-type: application/zip");
header("Content-Disposition: attachment;
filename=$archive_file_name");
header("Content-length: " . filesize($archive_file_name));
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
echo '<div class="alert alert-danger">
<strong>Error!</strong> Problem with your order details.'.$track.'
</div>';
} else {
echo 'Failed!';
}
}

Related

readfile returns a corrupt zip download

I have the following code in my Phalcon application.
public function downloadAction($key = NULL) {
if ($key == NULL) {
return $this->respondDownloadFailed();
}
$url = Urls::findFirst("token = '".$key."'");
if ($url) {
$path = $url->getUrl();
$ext = pathinfo($path, PATHINFO_EXTENSION);
header("Content-type: application/".$ext);
header("Content-Disposition: attachment; filename=".$key.".".$ext);
header("Content-length: " . filesize($path));
header("Pragma: no-cache");
header("Expires: 0");
ob_clean();
flush();
readfile($path);
unlink($path);
die;
}
return $this->respondDownloadFailed();
}
Now, when the .zip is created on the server I can open and unzip it. However, when I download it through the code above, I get a zip file which then unarchives to a .cpgz file. When unarchiving that one, I get a .zip again. Any ideas as to what is going wrong?

$zip -> addFile not working

Trying to create a zip-file with different pdf-files where I get the names on the pdf-files from checkboxes in a form.
I get the code to download a zip-file, but when I try to open it it say that
the zip is invalid. Sometimes I get no error but a empty folder. So it seems like $zip->addFile does not add the file.
Here is the code:
<?php
$test = $_POST['check'];
$zip = new ZipArchive;
if (!empty($_POST['check']))
{
$tmp_file = 'zip/myzip.zip';
if ($zip->open('myzip.zip') === TRUE)
{
foreach($_POST['check'] as $check)
{
$zip->addFile('pdf/' . $check, $check);
}
echo $tmp_file;
$zip->close();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"" . $tmp_file . "\"");
header("Content-Transfer-Encoding: binary");
readfile($tmp_file);
ob_end_flush();
} else {
echo 'failed';
}
} else {
echo 'Ingen vald';
}
?>

php add file to zip returning blank files

I am having an issue with the below code (it works fine on local).
I am using this for an image gallery to download a selection of images using a checkbox. Anyways, this is working fine on my localhost but when I execute this code on my live site it downloads a zip file with the correct file names but the the files are blank.
Any suggestions to why this may be happening?
<?php
function zipFilesAndDownload($file_names, $archive_file_name, $file_path)
{
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== true) {
exit("cannot open <$archive_file_name>\n");
}
foreach ($file_names as $files) {
# download file
$download_file = file_get_contents($files);
#add it to the zip
$zip->addFromString(basename($files), $download_file);
}
$zip->close();
$zipped_size = filesize($archive_file_name);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Type: application/force-download");// some browsers need this
header("Content-Disposition: attachment; filename=$archive_file_name");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Length:" . " $zipped_size");
ob_clean();
flush();
readfile("$archive_file_name");
unlink("$archive_file_name"); // Now delete the temp file on the server
exit;
}
if (isset($_POST['formSubmit'])) {
//$file_names=$_POST['files'];//
//$file_names = filter_var_array($_POST['files']);//works but it's the wrong method
$filter = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS); //sanitise files
$file_names = $filter['files'];
//Archive name
$archive_file_name = 'product-images.zip';
//Download Files path
$file_path = getcwd() . '';
//cal the function
zipFilesAndDownload($file_names, $archive_file_name, $file_path);
} else {
header("Refresh: 5; url= ./index.php ");
print '<h1 style="text-align:center">You you shouldn\'t be here ......</pre>
<p style="color: red;"><strong>redirection in 5 seconds</strong></p>
<pre>';
exit;
}
?>
edit: i see this in my php error log:
PHP message: PHP Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in /var/www/html/wp-content/plugins/jig/download-product-images.php on line 19

Archive unknown or damage error while compressed file in php

I'm trying to write code to zip a file and download it. My problem is when I download and open the file, I get the error
The archive is either in unknown format or damaged
Here's my code:
<?php
try{
$db_conn = new DBController();
$db_conn->prepare("SELECT filetypename FROM dirlistening WHERE dadroot = :BasName AND sonbas = :BasDirName");
$db_conn->bind(':BasName', $parentBasIndex);
$db_conn->bind(':BasDirName', $currentBas);
$db_conn->execute();
$dbzipoutput = $db_conn->getAll();
$db_conn->free();
$files = array();
if(!is_null($dbzipoutput)){
foreach($dbzipoutput as $azip){
$files[] = $azip->filetypename;
}
}
//$file_names = array("'" . implode( $filess, "','" ) . "'");
$file_names = $files;
function zipSourceFilesDownload($file_names,$archive_file_name,$file_path){
$zip = new ZipArchive();
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE){
exit("cannot open ".$archive_file_name."\n");
}else{
foreach($file_names as $filelist){
$zip->addFile($file_path.'/'.$filelist);
$zip->addEmptyDir($file_path.'/'.$filelist);
$zip->addFromString($file_path.'/'.$filelist, file_get_contents($file_path.'/'.$filelist));
$zip->setArchiveComment("zipped on ".date('Y-M-d')."
\nDownloaded from: My website
\nWebsite: http://www.example.com
\nProject Source: ".$file_path."
\nProject Url: http://example.com" . $_SERVER['REQUEST_URI']."
\nFILE COMMENT AND DESCRIPTION
\n none");
}
$var = $zip->close();
$download_file = true;
}
if($download_file){
ob_get_clean();
header("Pragma: no-cache");
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='.$archive_file_name);
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($archive_file_name));
print $zip_file;
unlink($archive_name);
}
return $var;
}
zipSourceFilesDownload($file_names,$archive_file_name,$file_path);
}catch (PDOException $e){ echo 'Connection failed: ' . $e->getMessage();}

Add files to zip with php

my php code for create zip file is:
function zipFilesDownload($file_names,$archive_file_name,$file_path)
{
$zip = new ZipArchive();
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open <$archive_file_name>\n");
}
foreach($file_names as $files)
{
if (!file_exists($files)) { die($files.' does not exist'); }
if (!is_readable($files)) { die($files.' not readable'); }
$zip->addFile($file_path.$files,$files);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$archive_file_name");
header("Pragma: no-cache");
header("Expires: 0");
readfile("$archive_file_name");
exit;
}
if ($act=="backup") {
$fileNames=array('us/man.txt');
$zip_file_name='myFile.zip';
$file_path=dirname(__FILE__).'/';
zipFilesDownload($fileNames,$zip_file_name,$file_path);
}
i add this code in my php, page show this code instead download:
PK0�~E1x��us/man.txtKIPK0�~E1x��users/mohammad-ali/mamad.txtPKJ>
but if i add it in another php, zip file downloaded correctly.

Categories