ZipArchive Multile Files/Folders - php

I have three files hash_a.php,hash_b.php,/morefiles/hash_c.php in root directory.
I want to zip it to client and stream it but not in that manner.
I want to send the client something like
FILE.ZIP
root
|_folder1
|_a.php
|_b.php
|_c.php
is it possible using php ?

First of all, you need to create a function something like the below code. And pass all the files to the function.
function create_zip($files = array(),$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) {
$zip->addFile($file,$file);
}
//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;
}
}
After creating the function. Pass the files to the function which you need to be zip. The code will looks like this.
$files_to_zip = array(
'preload-images/1.jpg',
'preload-images/2.jpg',
'preload-images/5.jpg',
'kwicks/ringo.gif',
'rod.jpg',
'reddit.gif'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
This code will make the .Zip file with the files that you need.

Related

Issue with Arabic file names while compress with Codeigniter zip library

I have certain files to compress and some of them have Arabic names, Upon extraction, they come out corrupted.
My PHP version is 7.2.18 and I am using the Codeigniter 3.1 framework.
Actual file name : كلمة العينة.docx
Compressed filename : +â+ä+à+¬_+º+ä+¦+è+å+¬.docx
public function generate_zip() {
$this->load->library('zip');
$zip_files = $this->document_model->get_documents();
foreach ($zip_files as $zip_file) {
$this->zip->read_file('uploads/documents/' . $zip_file->document_file_name);
}
$this->zip->download('temp.zip');
}
Hi Please try with the below code.
/* creates a compressed zip file */
function create_zip($files = array(),$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) {
$zip->addFile($file,$file);
}
//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;
}
}
$files_to_zip = array(
'عينة.txt',
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');

php zip all the files and folders on the same directory

how do I zip all the files and folders on the same directory using php zip ?
so basically I have a list of files and folders and would like to zip everything with php. sorry I am not familiar with php zip and the documentation on php net didnt help much
You can use this function to make zip folder of files:
public function create_zip($files = array(),$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) {
$zip->addFile($file,$file);
}
//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;
}
}
Here, $files will contain all files array. and $destination will be you zip file location over the server.

zip with php in root directory

I need to create a zip file in php, including in it some doc and pdf files. I use a function found on the web:
function create_zip($files = array(),$destination = '',$overwrite = true) {
//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)) {
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
foreach($valid_files as $file)
$zip->addFile($file,$file);
$zip->close();
return file_exists($destination);
}
else
return false;
}
The problem is that if the file path to include into the zip is path/to/file/filename.pdf when I download and open the zip i get filename.pdf contained into path/to/file folders. How can I do to put all the files in the root of the zip?
Use the second parameter of ZipArchive::addFile() to specify under which file name the file appears in the archive. basename($file) is a good candidate for that.

Best way to properly name files within zip archive

I have a function that creates zip files when passed an array of files which works beautifully.
$zip_file = create_zip($_FILES['myfile']['tmp_name'],$target);
But what happens is that the files in the zip archive all have the tmp names and no extensions. What would be the best way to alter the array I pass to the function such that the files are named the same way as when they are uploaded?
I've rewritten create_zip to include a localnames parameter. Pass in the $_FILES['myfile']['name'] for the files' original names.
function create_zip($files = array(),$localnames=array(),$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 to archive
for ($i = 0; $i < count($valid_files); $i++) {
$zip->addFile($valid_files[$i],$localnames[$i]);
}
//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;
}
}
Usage:
$zip_file = create_zip($_FILES['myfile']['tmp_name'], $_FILES['myfile']['name'],
$target);

Make Zip File of files from different location in php

function create_zip($files = array(),$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) {
$zip->addFile($file,$file);
}
//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;
}
}
// using function as
include('functions.php');
$files_to_zip = array(
'database/350-001(3.1).examdb',
'setup/ExaminationEngine.exe'
);
//if true, good; if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
I am using this function to make zip.
Every thing is working perfect but the files that are created under zip file is created by full path i want only my two file in my-archive.php. but inside my-archive.php datbase/350-001(3.1).examdb and setup/ExaminationEngine.exe database and setup folder is created.
How can i fix this?
try
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,basename($file));
}
instead of
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
second argument of addFile is 'local path', basename of $file is what you want to pass to it (I assume that right now you are passing some kind of relative path)
Remember that you will have to manually assert that your filenames are unique.
see
http://php.net/manual/en/ziparchive.addfile.php
http://php.net/manual/en/function.basename.php
I recommend that you prepare a temporary folder, copy the files from different locations in there, zip it, and remove the temporary folder. Doing otherwise sounds pretty boring.

Categories