How to create a zipfile with child zipFiles in php - php

i need to create a zipFile with a childs zipFiles already exist :
> folder
- file1.zip
- file2.zip
I want to get a zip file containing all the zip files in the folder.
$dir = "path/to/my/dir";
$finder = new Finder();
$zip = new \ZipArchive();
/***** I create a empty zip file ****/
$filesystem->dumpFile("$dir/delivrables.zip",'');
if($zip->open('delivrables.zip', ZipArchive::CREATE) === TRUE) {
foreach ($finder->in($dir) as $file) {
$zip->addFile($file->getPath(), $file->getFilename());
}
}
I don't get error but i can't extract zip file.

You can create a zip file with zip files in the same way as you create a zip file with images, documents etc

Related

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

Creating a zip file in a specific path without any subfolders

I need to create a Zip file in a specific file path without any sub folders created inside it,
here is my code :
$files = array('article/includes/pdfFiles/file1.txt','article/includes/pdfFiles/file2.txt');
$zipname = 'article/includes/pdfFiles/file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
I need the file.zip to be created in the path 'assets/uploads/pdfFiles' in this folder, its been created but the problem is inside the zip file these folders exits: assets/uploads/pdfFiles
How can I create the zip file without these folders?
Your code above doesn't work because you don't show the contents of $fileNames but I am assuming it is something like:
array('assets/uploads/pdfFiles/file1.txt','assets/uploads/pdfFiles/file2.txt',...)
When you add to the zip do this:
foreach ($fileNames as $fullpath) {
// pull just the file name from the path
$filename = substr($fullpath, strrpos($fullpath, '/')+1);
$zip->addFile($fullpath,$filename);
}

Create a zip in specific Folder PHP

I want create a zip in a specific Directory. Actually i can create a folder with this code:
function createZip($array)
{
$files = $array;
$zipname = 'ftpack.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
print_r($array);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
But this code create a zip in the principal directory, i want create in the at /zipDownload directory.
Just pass the full path:
$zipname = '/path/to/ftpack.zip';
Also make sure that the folder /path/to is writable by PHP.

create zip file inside a folder but without folder inside that zip

$File = "images/files.txt";
$zip = new ZipArchive();
$filename = "./images/files.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFile("$File");
$zip->close();
This code creates a files.zip file inside 'images' folder, if I open that zip file, 'images' folder is there too. I don't want folder 'images' to be there. But only the 'files.txt' file(located inside images folder) needs to be there.
Files structure:
zip.php
images
files.txt
How can I do that?
#hek2mgl I have 'files.txt' inside 'images' folder, that's why it's happening
Then your code will not work at all as the path to $File is wrong. Use this:
$File = "images/files.txt";
$zip = new ZipArchive();
$filename = "./images/files.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
// use the second parameter of addFile(). It will give the file
// a new name inside the archive. basename() returns the filename
// from a given path
$zip->addFile("$File", basename($File));
if(!$zip->close()) {
die('failed to create archive');
}

file_put_contents in to new directory

I have the following code, which as you can see i use to create a new directory then unzip a file.
<?php
function unzip_to_s3() {
// Set temp path
$temp_path = 'wp-content/uploads/gravity_forms/1-9e5dc27086c8b2fd2e48678e1f54f98c/2013/02/tmp/';
// Get filename from Zip file
$zip_file = 'archive.zip';
// Create full Zip file path
$zip_file_path = $temp_path.$zip_file;
// Generate unique name for temp sub_folder for unzipped files
$temp_unzip_folder = uniqid('temp_TMS_', true);
// Create full temp sub_folder path
$temp_unzip_path = $temp_path.$temp_unzip_folder;
// Make the new temp sub_folder for unzipped files
if (!mkdir($temp_unzip_path, '0755', true)) {
die('Error: Could not create path: '.$temp_unzip_path);
}
// Unzip files to temp unzip folder, ignoring anything that is not a .mp3 extension
$zip = new ZipArchive();
$filename = $zip_file_path;
if ($zip->open($filename)!==TRUE) {
exit("cannot open <$filename>\n");
}
for ($i=0; $i<$zip->numFiles;$i++) {
$info = $zip->statIndex($i);
$file = pathinfo($info['name']);
if(strtolower($file['extension']) == "mp3") {
file_put_contents(basename($info['name']), $zip->getFromIndex($i));
} else {
$zip->deleteIndex($i);
}
}
$zip->close();
}
unzip_to_s3();
?>
The unzip code was courtesy of #TotalWipeOut from one of my other posts. It currently unzips just mp3 files to my base directory, but i want to put them in my newly created folder.
I'm very new to PHP so have been trying my best with this, but i can't figure out how to change the file_put_contents(basename($info['name']), $zip->getFromIndex($i)); line to get it to put the files in my new folder?
As Marc B. mentioned you need to include the path to the directory you are putting the file in.
using your code:
file_put_contents($temp_unzip_path."/".basename($info['name']), $zip->getFromIndex($i));
I would also suggest reading a little more about the basics of PHP.

Categories