<?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
Related
I got this code but it doesn't work..
The problem with this code is that it downloads an empty zip file ...
I want to download all product images (either zip file or download them without compression)
public function downloadcatalog(){
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
foreach ($results as $result) {
$files[] = 'image/'.$result['image'];
}
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($files as $file) {
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
$file_name = $product_info['name'].'.zip';
# send the file to the browser as a download
/*header('Content-disposition: attachment; filename="'.$product_info['name'].'"');
header('Content-type: application/zip');*/
header("HTTP/1.1 200 OK");
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/zip");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($tmp_file));
//$zip->close();
//readfile($zipname);
readfile($tmp_file);
unlink($tmp_file);
}
When I tested the code below it is downloading the images with folders.
So, I believe your temp folder creation and then adding to the zip folder are creating issues.
public function downloadcatalog()
{
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
foreach ($results as $result) {
$files[] = 'image/' . $result['image'];
}
$zipname = $this->request->get['product_id'] . '.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
}
eCommerce download
I am trying to zip a series of pdf files automatically when a page loads. The files are based on a query. I can't seem to figure out the error. All of the pdfs are kept in the agreements folder and I want the zip file to just download on the viewer's computer. The error code that I get is:
agreements/Acadience_Cambridge_Post_1.pdfagreements/DoTheMath_Cambridge.pdfagreements/Edulog_Cambridge.pdfagreements/DESSA_Cambridge.pdfagreements/FuelEducation_Cambridge_signed.pdfagreements/LegendsofLearning_Dedham.pdfagreements/workforce19-22.pdfagreements/Ellevation_Dedham_Posting.pdfagreements/Yabla_Cambridge.pdfagreements/Breakthrough-2020.pdfagreements/Cedar-Labs-DPA.pdfagreements/Tutoring Plus.pdfagreements/Elefante Letrado_Brockton - post.pdfagreements/CollegeBoard_Cambridge_post_1.pdfagreements/LanguageConnections_post.pdfagreements/Flat_Westford - post.pdfagreements/Albert.io_Cambridge_post.pdfagreements/Starfall_Walpole.pdfagreements/Tynker_Milton - post_1.pdfagreements/WeVideo_CambridgeTEC_1.pdfagreements/Checkology_Dedham.pdfagreements/BabylonHouse _Cambridge_signed_4.pdfagreements/FreeMath_Westwood_Post.pdf
Warning: ZipArchive::close(): Failure to create temporary file: Permission denied in /var/www/html/extract_dpas2.php on line 36
Could not find Zip file to download
Here is the code I have:
<?php require_once('Connections/sdpc_i.php'); ?>
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
$sql = "SELECT * FROM data WHERE districtID = 457 and statusID=1 and signed_agreement_file != 'NULL'";
if ($result=mysqli_query($sdpc_i,$sql))
{
// $rowcount=mysqli_num_rows($result);
// printf("Result set has %d rows.\n",$rowcount);
//define array for storing files
$file = array();
while ($row = mysqli_fetch_assoc($result)) {
//give file name with extension
$name ="agreements/".$row['signed_agreement_file'];
//store file name in array
$file[] = $name;
// store file in one folder named 'files'
file_put_contents($name,$row["signed_agreement_file"]);
}
$zipname = "file.zip";
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
foreach ($file as $f) {
echo $path = "".$f;
if(file_exists($path)){
$zip->addFromString(basename($path), file_get_contents($path));
}
else{
echo"file does not exist";
}
}
$zip->close();
//check zip file create or not and download it
if (file_exists($zipname)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($zipname).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($zipname));
ob_clean();
flush();
readfile($zipname);
exit;
} else {
exit("Could not find Zip file to download");
}
mysqli_free_result($result);
mysqli_close($conn);
}
?>
Thank you in advance for you help.
Hi I want to show an image gallery on my site and then have a link that allows the user to download the whole image gallery
All the image automatically convert to the zip file & displayed button on product page to "download image"
Like This :-
Product.php
Create new function
public function downloadcatalog(){
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
foreach ($results as $result) {
$files[] = 'image/'.$result['image'];
}
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($files as $file) {
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
$file_name = $product_info['name'].'.zip';
# send the file to the browser as a download
/*header('Content-disposition: attachment; filename="'.$product_info['name'].'"');
header('Content-type: application/zip');*/
header("HTTP/1.1 200 OK");
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/zip");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($tmp_file));
//$zip->close();
//readfile($zipname);
readfile($tmp_file);
unlink($tmp_file);
}
====================================================================
Product.php
(Index controller add before)
$data['column_left'] = $this->load->controller('common/column_left');
$data['download_catalog'] = $this->url->link('product/product/downloadcatalog', 'product_id=' . $this->request->get['product_id']);
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
I have problem in downloading file(php)from a particular folder.
when i download and open the file it says your file is corrupted.
when i check the size of the uploaded file and downloaded file it is same , but for zip file size it differs.
No Files are opening.
can any one say where i am wrong???
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
}
else
{
$filename = NULL;
}
$err = 'Sorry, the file you are requesting is unavailable.';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
}
else
{
// define the path to your download folder plus assign the file name
$path = '/public_html/wp-content/uploads/'.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octet-stream');
header('Content-Length: '.$size);
header('Content-Disposition: attachment;filename="'.basename($filename).'"');
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file canĀ“t be opened
$file = # fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
exit;
}
inserting into table:
echo "<tr><td><a href='?file=" . $row["FileupName"]. "'>".$row["FileupName"]."</td></tr>";
I am happy that the file is getting downloaded but it not getting opened.
.txt file is getting opened.
Had checked with header also.
i have tried putting:
ob_clean();
flush();
readfile($file);
if (file_exists($path)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($path));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
ob_clean();
flush();
readfile($path);
exit;
}