$zip -> addFile not working - php

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';
}
?>

Related

Downloading zip file automatically via php code

<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);

cant download zip file from php function

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!';
}
}

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();}

download file using php, file is stored in a folder and name of the file is stored in database

I am trying to download a file from mysqli database using php for that i have stored file in a folder and file name is stored in the mysqli database.
here is my code...
$query= mysqli_query($dbo,"SELECT * FROM diff_questions WHERE email = ' $email ' and status= '0' ");
$rows=mysqli_fetch_array($query);
$file=$rows['file'];
$path='http://localhost/admin1/uploads';
?>
echo "<a href='download.php?file=".$file."&path=".$path." '>Download File</a> ";
and now
download.php-
<?php
$file = $_GET["file"];
$path = $_GET["path"];
$fullfile = $path.$file;
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . Urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . Filesize($fullfile));
flush();
$fp = fopen($fullfile, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush();
}
fclose($fp);
?>
Can someone please tell me what is wrong with code. Its not working .
i mean it working file till saving file but its not downloading full file imean afther downloading file size of file is 0kb
Once replace your download.php with following code and check if its working for you or not :
if(isset($_GET["file"])){
$file = $_GET["file"];
$path = $_GET["path"];
$fullfile = $path.$file;
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . Urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
$fp = fopen($fullfile,'r');
while ($line = fgets($fp)) {
echo($line);
}
fclose($fp);
}

Php or Apache? Content of zip file is diplayed instead of downloading zip 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;

Categories