Add files to zip with php - 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.

Related

php zip adding non selected files

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?

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

create a zip but cannot extract

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

Zipping checkboxed files PHP

<?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

Zip Multiple file Than unlink the file

<?php
$direc = base64_decode($_GET['path']);
$zip = new ZipArchive();
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();
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=" . $archive_file_name);
header('Cache-control: private, must-revalidate');
header('Content-Transfer-Encoding: binary');
readfile("$archive_file_name");
exit;
}
if (isset($_POST['file'])) {
$file_names = $_POST['file'];
$archive_file_name = 'zipped.zip';
$file_path = $direc;
echo $file_path;
//Run Function above
zipFilesAndDownload($file_names, $archive_file_name, $file_path);
if (file_exists($archive_file_name)) {
unlink($archive_file_name);
}
?>
I find code above and try it, The zip file created but statement unlink($zipname); doesn't work
I don't have any problem about permission
Is that file strill write-protected?
How to solve that problem?
Thanks Friends
Your function above should call return, not exit. Exit terminates execution of the script, so nothing after that will be called. It's probably not even getting to the file_exists part.

Categories