I found a great solution on how to ZIP folders and files on my server, but how do I make the script exclude the folder /archiv and all of its subfolders?
$backup_file = $_SERVER['DOCUMENT_ROOT'].'/archiv/system/system_' . date("Y-m-d-H-i-s") . '.zip';
$zip = new ZipArchive();
$zip->open($backup_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($_SERVER['DOCUMENT_ROOT']),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
if($file == 'archiv'){
continue;
}else{
// Skip directories (they would be added automatically)
if (!$file->isDir()){
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$zip->addFile($filePath, $relativePath);
}
}
}
$zip->close();
Many thanks for any help!
Related
zipping the video files works fine, but I want to add a password to a zipped file instead of individual files that have been zipped. Below is my code:
public function zipVideos()
{
$videos_zip = "videos.zip";
$zip = new ZipArchive();
$zip->open($videos_zip, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->setPassword('secret');
$path = $this->getPath("videos");
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($files as $name => $file) {
if (file_exists($file)) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = "videos" . DIRECTORY_SEPARATOR . basename($filePath, strlen($path) + 1);
$zip->addFile($filePath, $relativePath);
// I don't want this individual video file to be password-protected
$zip->setEncryptionName($relativePath, ZipArchive::EM_AES_256);
}
}
}
// A file i want to be password-protected should be the zipped file - videos.zip itself instead
$zip->setEncryptionName($videos_zip, ZipArchive::EM_AES_256);
$zip->close();
return redirect("/media")->banner("All videos have been zipped successfully");
}
I need your guide. Thanks in advance
NB: Please I use Laravel framework.
I'm trying to make a existing folder to a zip and download it using php. The existing folder contain some files. This is my code
$folder = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.']';
$zip_name = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.'].zip';
$zip = new ZipArchive;
if($zip -> open($zip_name, ZipArchive::CREATE ) === TRUE) {
$dir = opendir($folder);
while($file = readdir($dir)) {
if(is_file($folder.$file)) {
$zip -> addFile($folder.$file, $file);
}
}
$zip ->close();
}
But when i called the API zip folder is not created. Please go through my code and help me to solve it. Thanks in advance
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
Try below code.
$folder = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.']';
$zip_name = 'excel/report/[line: '.$args_line.'][startDate: '.$start_date.'][endDate: '.$end_date.'].zip';
$zip = new ZipArchive;
$zip->open($zip_name, ZipArchive::CREATE);
foreach (glob($folder) as $file) {
$zip->addFile($file);
}
$zip->close();
This question already has answers here:
How to zip a whole folder using PHP
(20 answers)
Closed 1 year ago.
I'm trying to download a zip from a folder that i have on my app (Laravel 5.8), it's working but they skip all empty folders, and I need them in the zip.
I already tried ZipArchive (php) and chumper/zipper from composer.
Any idea of how can i do this?
This is a Linux Server, running MySQL 5, PHP 7.2 and Apache2.
$files = glob($folder_path);
\Zipper::make($folder_path.'/'.$folder_main_name.'.zip')->add($files)->close();
This lib only accepts glob, but if you have any solution that works, i can easily abandon this.
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zipFilename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
if (!$file->isDir())
{
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}else {
if($relativePath !== false)
$zip->addEmptyDir($relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
Hi. I modified the previous answer and now I get the zip file including the empty folders. I hope this helps.
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** #var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Is this a directory?
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
else {
$end2 = substr($file,-2);
if ($end2 == "/.") {
$folder = substr($file, 0, -2);
$zip->addEmptyDir($folder);
}
}
}
// Zip archive will be created only after closing object
$zip->close();
** Try this. Off of the top of my head, not tested. But its the general idea.
I am making a zip-archive from directory mydir stored in /my/files/inside/here/.
I want the archive mydir.zip to contain only the files and dirs from mydir.
The problem, however, is that i now get the full rooth path /my/files/inside/here/mydir/ when I unzip..
$mypath = "/my/files/inside/here/";
$dirname_to_zip = "mydir";
$zip = new ZipArchive();
$zipfile_with_path = $mypath.$dirname_to_zip.".zip";
if ($zip->open($zipfile_with_path, ZipArchive::CREATE)!==TRUE)
die("failed to create zip");
// Create recursive directory iterator
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($mydir.$dirname_to_zip),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($dirname_to_zip) );
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
How can I remove the root path?
I create zip file in windows with this code
$plugin_address="D:/processmaker-3.2.1-x/apps/processmaker/htdocs/cakephp/plugins";
$rootPath = $plugin_address."/".$R;
$zipFileName = $rootPath.'.zip';
$zip = new ZipArchive();
$zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
if (!$file->isDir())
{
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$filePath=str_replace("\\","/",$filePath);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
Zip file is correct...(image above)
Now i want extract this file in Ubuntu with this code
$zipAdress="/var/www/cakephp/plugins/backup/EstelamBasic.zip";
$plugin_address="/var/www/cakephp/plugins/EstelamBasic/";
$zip = new ZipArchive;
$res = $zip->open($zipAdress);
if ($res === TRUE) {
$zip->extractTo($plugin_address);
$zip->close();
}
This code worked and extract zip file but does not create directory and sub directory.
This code set directory name in file name!
(extract code in windows is correct and create directory and sub directory and set file in directory)
I use this function for zip on windows that worked extract on linux
$rootPath = "/var/www/cakephp/plugins/EstelamBasic";
$zipFileName = $rootPath.'.zip';
$zip = new ZipArchive();
$zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
addFolderToZip($rootPath."/", $zip, $zipdir = '');
$zip->close();
function addFolderToZip($dir, $zipArchive, $zipdir = ''){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//Add the directory
if(!empty($zipdir)) $zipArchive->addEmptyDir($zipdir);
// Loop through all the files
while (($file = readdir($dh)) !== false) {
//If it's a folder, run the function again!
if(!is_file($dir . $file)){
// Skip parent and root directories
if( ($file !== ".") && ($file !== "..")){
addFolderToZip($dir . $file . "/", $zipArchive, $zipdir . $file . "/");
}
}else{
// Add the files
$zipArchive->addFile($dir . $file, $zipdir . $file);
}
}
}
}
}