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

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

Related

PHP Create Zip destination to a folder not on root

I have a code to create a zip of a folder but the zip file goes to the root i want to create the zip in a folder like "zips" or something else not in the root... This is the code.
//Create Zip
function create_zip($files=Null,$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
//zip
$error = ""; //error holder
$file_folder = "uploads/tracks/"; // folder to load files
if(extension_loaded('zip')){ // Checking ZIP extension is available
$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($post['files'] as $file){
$new_filename = substr($file,strrpos($file,'/') + 1);
$zip->addFile($file,$new_filename); // 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 .= "* You dont have ZIP extension<br/>";
}
}
All i want is to send the zip files to a folder not the root.
function create_zip($files=Null,$destination = '',$overwrite = false)
Here at $destination give your location or folder name where you want to store the file.

php downloaded files in zip are empty

currently I am trying to put files in a zip and download them. I use the following code:
# create new zip opbject
$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();
# send the file to the browser as a download
header('Content-disposition: attachment; filename=Resumes.zip');
header('Content-type: application/zip');
readfile($tmp_file);
The files are added to the array the following way:
$weborder = $_POST['weborder'];
$printlocation = $_POST['print'];
$dir = "z:\Backup\\$printlocation\\$weborder.zip";
$zip = new ZipArchive;
$files = array();
if ($zip->open($dir))
{
for($i = 0; $i < $zip->numFiles; $i++)
{
if ($zip->getNameIndex($i) != "order-info.txt" && $zip->getNameIndex($i) != "workrequest.xml" && $zip->getNameIndex($i) != "workrequest.pdf")
{
$filename = $zip->getNameIndex($i);
$files[$i] = $dir . "\\" . $filename;
}
}
}
This downloads the zip and the files that are in the zip. The only problem I am having is that the files are empty.
instead of
$zip->addFromString(basename($file),$download_file);
try
$zip->addFile($basename($file));
This code is working make sure that your files are existed or not.
$array = array("sites/README.txt","sites/chessboard.jpg"); //files to Add/Create in zip file
$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($array as $key => $value)
{
if(file_exists($value)){
$zip->addFile($value); // Adding files into zip
}else{echo $value ." file not exist<br/>";}
}
$zip->close();
if(file_exists($zip_name))
{
echo "yes";die;
// 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{echo "zip not created";die; }
For Download Existing file
$zip_name = "YOUR_ZIP_FILE PATH";
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{
echo "zip not created";exit;
}

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

PHP Zip and download files from multiple folder

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ยข

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