PHP Zip and download files from multiple folder - php

So I am doing a localhost website for fonts, and I managed to let the user select multiple files into a cart, it can be download if they are from the same folder for example either C:/fonts/a or C:/fonts/b but if there are files from both a and b, it can't be downloaded
So is it possible to do that? like download selected files from both a and b
Below is my code for download.php
<?php
include 'dbfunctions.php';
if (!$link) {
die(mysqli_error($link));
}
$sql="SELECT id, Font_Name, Font_Family
FROM font";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
while($row = mysqli_fetch_array($result)){
$Font_Family = $row['Font_Family'];
$a = file_get_contents('./a.txt');
$file_dir = file_get_contents('./font_path.ini');
$path = $file_dir.$Font_Family.$a;
$error = ""; //error holder
if(isset($_POST['createpdf']))
{
$Font_Family = $_POST['Font_Family'];
}
$file_folder = $path;// folder to load files
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
if(isset($_POST['files']) and count($_POST['files']) > 0)
{
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($_POST['files'] as $file)
{
$zip->addFile($file_folder.$file,pathinfo($file,PATHINFO_BASENAME));
readfile($path);
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
$error .= "* Please select file to zip ";
}
else
$error .= "* You dont have ZIP extension";
}
?>
fontpath.ini is c:\fonts\
a is just \ since php gives me problem when I just write \
Font_Family is the different folder name inside c:\fonts\
so the path is like C:\fonts(whichever folder name here)\

You could take each font, regardless of the directory, and copy it to a temp dir, say, like, C:/fonts/temp/cartID/. You could then zip that directory of fonts (some from /fonts/a and some from /fonts/b) and provide the download. You then would need to garbage collect, that is, delete the temp cartID dir.
Just my 2ยข

Related

Getting error when archive files ' zipArchive::close() Read Error: no such file or directory IN c:/'using PHP?

I am using PHP to download a number of files as zip folder, see code below,
it shows me this error zipArchive::close() Read error: no such file or directory in C:// path
I have added HTML button on click to take the id of the row to download its files
if (isset($_GET['zip'])) {
$zip_id = $_GET['zip'];
$query4 = "select f.id f.file_name, f.path, r.user_id from register r "
. "join files f on f.register_id =r.user_id "
. "where r.user_id=$zip_id";
$result4 = mysqli_query($con, $query4);
if (($result4)) {
while ($row = mysqli_fetch_array($result4)) {
$document[$row['id']] = array(
'id' => $row['id'],
'file_name' => $row['file_name'],
'path' => $row['path']
);
}
}
// folder to load files
if (extension_loaded('zip')) {
// Checking ZIP extension is available
foreach($document as $f){
$files[]='uploads/'.$f['path'];
}
if ($files != null) {
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = 'folder.zip'; // Zip name
if ($zip->open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach ($files as $file){
$zip->addFile($file,$file);} // Adding files into zip
$zip->close();
if (file_exists($zip_name)) {
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="' . $zip_name . '"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
} else {
$error .= "* no file to zip ";
}
} else {
$error .= "* You dont have ZIP extension";
}
}
I found the error in my code. I have declared & initialized the $document array inside the while loop which is not defined for the zipArchive(). So, it cannot see it then it returns null.
the correction I have defined the creation of the $files inside the while loop.

Progress bar to prevent 504 Gateway TimeOut in ZIPARCHIVE::CREATE

I'm creating a zip with php with big files like videos. The ZIP should be around 500mb and the php crash with 504 Gateway TimeOut when i try to makeit. I want to add a progress bar or something to avoid the server saturation, i think the key is in the foreach when add files.
foreach($post['files'] as $file) {
$zip->addFile($file,basename($file));
}
i already setup max_execution_time
ini_set('max_execution_time', 0);
Any idea how is the best way?
Complete code:
<?php
set_time_limit(0);
ini_set('max_execution_time', 0);
if(isset($_POST['files']))
{ $error = ""; //error holder
{
$post = $_POST;
$file_folder = "files/"; // folder to load files
if(extension_loaded('zip'))
{
// Checking ZIP extension is available
if(isset($post['files']) and count($post['files']) > 0)
{
// Checking files are selected
$casting_titulo = $post['casting_titulo'];
$zip = new ZipArchive(); // Load zip library
$zip_name = $titulo."cast.zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($post['files'] as $file) {
$zip->addFile($file,basename($file));
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}}
else $error .= "* Please select file to zip "; }
else $error .= "* You dont have ZIP extension"; }
}
?>

Php multiple file download as zip and rename all files name under zip

I need to download all files form url and download as a zip. Every thing is working but i am not able to rename the file names under zip. I am using below code
$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
$error = "* Sorry ZIP creation failed at this time";
}else{
foreach ($_POST as $file) {
foreach($file as $res){
$download_file = file_get_contents($res);
$zip->addFromString(basename($res), $download_file);
}
}
$zip->close();
}
can anyone please help me how can i rename the files?
In your code, you use the line
$zip->addFromString(basename($res), $download_file);
...that means: add the downloaded file under it's name to the archive. If you want to change the file name that should occur in the archive, you should start looking here

Files not added to ZIP

Am developing a code that creates a ZIP file out of selected files from a folder. The folder is created with sessiontime as name, but the files are not added to the ZIP file. The permissions of files are 777. The files are getting selected, indicated by the output below. I have also checked the code by replacing with line $zip->addFile($file_folder); which downloads a blank zip archive, hence there is no issue with it.
<?php
ob_start();
$error = ""; //error holder
if(isset($_POST['createpdf']))
{
$post = $_POST;
$file_folder = "test/".$_SESSION["dir_name"]."/"; // folder to load files
echo $file_folder;
if(extension_loaded('zip'))
{ // Checking ZIP extension is available
echo "checking";
if(isset($post['files']) and count($post['files']) > 0)
{ echo "/checking2";// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
{
// Opening zip file to load files
echo "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['files'] as $file)
{ echo "added";
$zip->addFile($file_folder.$file); // Adding files
into zip
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
The output I get is the line : test//checking/checking2addedaddedadded
You forgot session_start() after opening the <?php tag, so your $_SESSION['dir_name'] variable is empty

how to create rar/zipfile and download [duplicate]

This question already has answers here:
Dynamically creating zip with php
(2 answers)
Closed 9 years ago.
Hi frnds can anyone tel me how to Create a Zip File Using PHP?
Actually i am having 10 files with boxes,if i select more than one check box that files should get zip/rar and i am able to save that in some path..
Please can anyone help in this solution as i am new to php
$zip_name = 'path/to/final.zip'; //the real path of your final zip file on your system
$zip = new ZipArchive;
$zip->open($zip_name, ZIPARCHIVE::CREATE);
foreach($files as $file)
{
$zip->addFile($file);
}
$zip->close();
header('Content-type: application/zip');
header('Content-disposition: filename="' . $zip_name . '"');
header("Content-length: " . filesize($zip_name));
readfile($zip_name);
exit();
// This example creates a ZIP file archive test.zip and add the file /path/to/index.txt. as newname.txt.
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
$zip->addFile('/path/to/index.txt', 'newname.txt');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
//include your connection file
//$file_names[] get your files array
//$error = ""; //error holder
//$file_path='set your image path' folder to load files
if(extension_loaded('zip'))
{ // Checking ZIP extension is available
if(count($file_names) > 0)
{
// Checking files are selected
$zip = new ZipArchive(); // Load zip library
$zip_name = time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($file_names as $file){
$zip->addFile($file_path.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name))
{
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
}
else
{
$error .= "* Please select file to zip <br/>";
}
}
else
{
$error .= "* You dont have ZIP extension<br/>";
}
?>

Categories