I'm having trouble moving all the files in the directory into a zip file. Below is my code. When I run the code, no zip file is created. What am I doing wrong?
$dir = "archive";
$files = array_diff(scandir($dir), array('.', '..'));
$zip = new ZipArchive();
$zipfile = "./test.zip";
$zip->open($zipfile, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
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();
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);
}
}
}
}
}
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!
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.