I had a set of files in a folder. from this few will select and adding to zip. But it adding non selected files also.
The sample file names are
EXPLORER_TRM_FDM_147461_B_280.pdf,
EXPLORER_TRM_FDM_147463_B_130.txt,
EXPLORER_TRM_FDM_147470_B_130.pdf,
From this I am selecting only second one but all are coming.
my function is
public 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){
$zip->addFile($file_path.strtolower($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;
}
$file_names contain only
EXPLORER_TRM_FDM_147463_B_130.txt
but the other two also adding into zip. where I am wrong?
Related
I have blob stored in Database and successfully converted it into PDF. I am using this code.
header('Content-type: application/pdf');
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Disposition: attachment;filename=".$this->pdfName);
header("Content-length: ".strlen($res[0]['comp_statement']));
$this->pdf = $res[0]['comp_statement']
echo $res[0]['comp_statement'];
There are two option in Content-Disposition ethier you can download it or show it on web page.
I do not want to show or download. I want it to place in zip directory which I am creating dynamically.
Here the code for zip archive
$zip = new ZipArchive;
$zipName = $this->currentCompYear.'.zip';
if ($zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
if(!$zip->locateName($this->currentCompYear)) {
$zip->addEmptyDir($this->currentCompYear);
}
if(!$zip->locateName($regionDir)) {
$zip->addEmptyDir($regionDir1);
}
if(!$zip->locateName($managerDir)) {
$zip->addEmptyDir($managerDir1);
}
$zip -> addFile($this->pdf));// I want to add my pdf file here
$zip->close();
ob_clean();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zipName);
header("Content-length: " . filesize($zipName));
header("Pragma: no-cache");
header("Expires: 0");
readfile($zipName);
Any Clue.
You dont need any of that header and echo stuff if you just want to store the contents to a file, use file_put_contents() to store the pdf, then pass the file name and not the contents to addFile() of your Zip function
https://www.php.net/manual/en/function.file-put-contents.php
I managed to zip the files and downloaded it. The only problem is that I cant extract the zip file is says "An error occured while loading the archive". It contains data since it has around 1000kb. Is there something wrong with my code?
$zip = new ZipArchive();
$zip_name = "zipfile.zip";
if($zip->open($zip_name, ZipArchive::CREATE) !== TRUE) {
echo("Zip failed");
} else {
foreach($temp_file as $file) {
$zip->addFile($file, $file);
}
$zip->close();
header('Content-disposition: attachment; filename=media.zip');
header('Content-type: application/zip');
readfile($zip_name);
}
I've added a variable $zipname to contain the full path to your "zipfile.zip". It is later used to determine the filesize, which is send with the content-length header. Please adjust the path!
added some more headers, especially length and pragma
adjusted length and disposition to use dynamic filenames based on the variables on the top
added exit() after readfile() - rare, but you never know the registered shutdown handlers and their output behaviour
keep in mind, if you have large files you are sending through readfile() and you have a busy site, this will consume memory
Give it a try:
<?
$zip = new ZipArchive();
$zip_name = "/path/to/zipfile.zip"; // <-- adjust path
$file_name = basename($zip_name);
if($zip->open($zip_name, ZipArchive::CREATE) !== TRUE) {
echo("Zip failed");
} else {
foreach($temp_file as $file) {
$zip->addFile($file, $file);
}
$zip->close();
header("Pragma: public");
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($zip_name));
header("Content-Transfer-Encoding: binary");
readfile($zip_name);
exit;
}
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.
<?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");
}
//add each files of $file_name array to archive
foreach($file_names as $files) {
$zip->addFile($file_path.$files,$files);
}
$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 (some servers need this option)
exit;
}
if(($_POST['formSubmit'])) {
//$file_names=$_POST['items'];// Always sanitize your submitted data!!!!!!
//$file_names = filter_var_array($_POST['items']);//works but it's the wrong method
$filter = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS) ;
$file_names = $filter['items'] ;
//Archive name
$archive_file_name= "zip.zip";
//Download Files path
$file_path= $leadon;
//cal the function
zipFilesAndDownload($file_names,$archive_file_name,$leadon);
}
?>
I have a working code here.
The main problem is
//Download Files path
$file_path= $leadon;
//cal the function
zipFilesAndDownload($file_names,$archive_file_name,$leadon);
This code
echo $leadon;
returns string images2/User/
And it doesn't create me the zip file ( well it creates a blank ) but if I manually type in the path
"images2/User/"
into
$file_path= and then I just change
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
everything works fine can you tell me why is it so? And how can I change it:s
I am trying to write a PHP program which delivers a zip file for download. I have searched on the internet and tried all the solutions but my issue is not getting solved.
My problem is when the zip file is downloaded to a user's computer the entire path is displayed as the name of the zip file.
For Example:
Path to the Zip File: "http://www.websitename.com/folder1/folder2/"
Name of Zip File: "zbc123.zip"
When the Browser downloads the zip file, the Name of the file is as follows:
http-_www.websitename.com_folder1_folder2_zbc123.zip
I do not want the path to be the name of the downloaded zip file.
I only want the actual zip file to be displayed.
Here is my codespec:
$m_item_path_name = "http://www.websitename.com/folder1/folder2/";
$m_zip_file_name = "zbc123.zip"
//Combining the Path and the Zip file name and storing into memory variable
$m_full_path = $m_item_path_name.$m_zip_file_name;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$m_zip_file_name."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($m_item_path_name.$m_zip_file_name));
ob_end_flush();
#readfile($m_item_path_name.$m_zip_file_name);
Can anyone help me to solve this issue.
Any kind of help will be appreciated.
Thanks a lot.
try this:
<?php
$file_names = array('file1.pdf','file2.pdf');
//Archive name
$archive_file_name=$name.'Name_u_want.zip';
//Download Files path
$file_path=$_SERVER['DOCUMENT_ROOT'].'/files/';
zipFilesAndDownload($file_names,$archive_file_name,$file_path);
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
//echo $file_path;die;
$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");
}
//add each files of $file_name array to archive
foreach($file_names as $files)
{
$zip->addFile($file_path.$files);
}
$zip->close();
//then send the headers to foce download the zip file
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");
exit;
}
?>