Adding all subdirectories to a zip archive - php

In this case I am trying to add all files and subdirectories to my zip file.
$zip = new ZipArchive;
$zip->open('wordpress.zip', ZipArchive::CREATE);
// Adding all files
foreach (glob(CLIENT_PATH . "/*.*") as $file) {
$filename = substr($file, strrpos($file, '/') + 1);
echo $filename . '<br>';
$zip->addFile($file, 'wordpress/wp-content/themes/' . $title . '/' . $filename);
}
// Adding all subdirectories
$directories = glob(CLIENT_PATH . '/*' , GLOB_ONLYDIR);
foreach (glob(CLIENT_PATH . '/*', GLOB_ONLYDIR) as $dir) {
$zip->addEmptyDir('wordpress/wp-content/themes/' . $title . '/' . $dir);
}
$zip->close();
Adding all files works perfectly well but adding all subdirectories won't work as expected.
This is how my unzipped file looks:
The subdirectory of my main directory is called assets and it also includes some more subdirectories and they also have some subdirectories. But as you see in the image above, assets includes just nothing. And also, I don't understand why it starts with home/pomcanys/ etc. instead of assets.
How can I fix this? Any help would be appreciated!

To automatically include files from the sub-directories use the -r parameter for recursiveness:
zip -r foo.zip *

Related

I need to create .Zip file in Laravel and my code does not work

this is the 1st time I posted on StackOverflow. I need to create a zip file of multiple images. I've tried Zipper and also ZipArchive and my code still fails.
$zip = new \ZipArchive();
foreach ($students as $student) {
$download = 'album' . $student->album_id . '.zip';
if ($zip->open(public_path('albums/' . $student->album_id . '/' . $download), \ZipArchive::CREATE) === TRUE) {
$files = Storage::allFiles('public/albums/' . $student->album_id . '/image_files');
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
}
I can assure that all the images exist. I put the images in Storage/app/public/albums/$student->album_id/image_files/. Please help me.
First, you can add an additional step to verify that your file exists by using PHP built in function: file_exists()
Most times, if you your files do noT exists, no files will be added to the zip, and your code will run without throwing any error but still will not work.
What you're passing to addFile() is not a correct path. addFile() needs an absolute path to where your file is stored. You will need to add this line $file_path = storage_path("app/{$file}");
See code below:
$zip = new \ZipArchive();
foreach ($students as $student) {
$download = 'album' . $student->album_id . '.zip';
if ($zip->open(public_path('albums/' . $student->album_id . '/' . $download), \ZipArchive::CREATE) === TRUE) {
$files = Storage::allFiles('public/albums/' . $student->album_id . '/image_files');
foreach ($files as $file) {
$file_path = storage_path("app/{$file}");
if (file_exists($file_path)) {
$zip->addFile($filepath);
}
}
$zip->close();
}
}
Optionally, if you wish to download any of the zipped files, you can return the download response at the end of your code to download:
return response()->download(public_path('albums/' . $student->album_id . '/' . $download));

Add files and directory to a specific sub directory

I am using the following script to move the files of my directory (in this case My_Theme) to the zip archive wordpress.zip.
define('CLIENT_PATH', $_SERVER['DOCUMENT_ROOT'] . '/wp_theme/clients_templates/' . str_replace(' ', '_', $_POST['title']));
$zip = new ZipArchive;
$zip->open('wordpress.zip', ZipArchive::CREATE);
foreach (glob(CLIENT_PATH . "/*.*") as $file) {
echo $file . '<br>';
$zip->addFile($file);
}
$zip->close();
Now when I download and unzip that file, my folder structure looks like this:
What I want is to move the directory My_Theme to wordpress/wp-content/themes/
The result would be: wordpress/wp-content/themes/My_Theme (including all the files and sub directories within)
How can I do this?
I answer my own question and the answer is easy: Just define the second parameter:
$zip->addFile($file, 'wordpress/wp-content/themes/' . $theme_name . '/' . $file_name);
You could use http://php.net/manual/en/function.rename.php. That should do what you're looking for.

Copy contents of a directory to lots of other subdirectories

I have a some template files organised like this in a directory:
index.php
preferences.xml
user.xml
I have also have subdirectory of this directory that contains a large number of folders (the exact number of folders can change a lot) which have outdated versions of the above template files:
randomfolder1
randomfolder20
randomfolder29
...
I would like to be able to copy the template files into all of these folders in the subdirectory, regardless of how many directories or template files there are. This is an example of what I would like in every folder:
randomfolder
- index.php
- preferences.xml
- user.xml
I have used this to copy the template files into ONE directory, but not all of them:
$cdire = 'GMXD/Users/firstdirectory';
mkdir($cdire);
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
recurse_copy('template',$cdire);
So how can you make this script copy all files in the template directory to all files in the subdirectory ?

PHP copy all images folder from one folder to another

I am working on some code that will take all the images in one folder and copy them into another folder and then delete the original folder and it content.
I have:
copy('images/old-folder/*', 'images/new-folder/');
unlink('images/old-folder/');
but this does not work :( but it doesn't work I mean the files dont copy over and the old-folder is not deleted :(
I even tried:
system('cp images/old-folder/* images/new-folder/');
and that didn't work either :( Please help.
I have even tried to change the permissions of the two folders:
chmod('images/old-folder/', 0777);
chmod('images/new-folder/', 0777);
foreach(glob('images/old-folder/*') as $image) {
copy($image, 'images/new-folder/' . basename($image)); unlink($image);
}
rmdir('images/old-folder');
check the docs: glob, rmdir, also you might find user comments on rmdir useful.
EDIT:
added basepath to the second parameter to the copy function which has to be an actual path and not a directory.
<?php
$src = 'pictures';
$dst = 'dest';
$files = glob("pictures/*.*");
foreach($files as $file){
$file_to_go = str_replace($src,$dst,$file);
copy($file, $file_to_go);
}
?>
Found here: PHP copy all files in a directory to another?
Here is a modified version from #Prasanth that should work (not tested)
<?php
$oldfolder = 'images/new-folder';
$newfolder = 'images/old-folder';
$files = glob($oldfolder . '/*');
foreach($files as $file){
$filename = basename($file);
copy($file, $oldfolder . '/' . $filename);
unlink($file);
}
rmdir($oldfolder);
?>

Move file into a specific folder

I have a question regarding file handle, i have:
Files:
"Mark, 123456, HTCOM.pdf"
"John, 409721, JESOA.pdf
Folders:
"Mark, 123456"
"Mark, 345212"
"Mark, 645352"
"John, 409721"
"John, 235212"
"John, 124554"
I need a routine to move files to correct folders.
In case above, i need to compare 1st and second value from file and folder. If are the same i move the file.
Complement to post:
I have this code, work right but i need to modify to check name and code to move files...
I'm confused to implement function...
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
foreach (glob($pathToFiles . DIRECTORY_SEPARATOR . '*.pdf') as $oldname)
{
if (is_dir($dir = $pathToDirs . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_FILENAME)))
{
$newname = $dir . DIRECTORY_SEPARATOR . pathinfo($oldname, PATHINFO_BASENAME);
rename($oldname, $newname);
}
}
As a rough-draft and something that will only work with your specific case (or any other case following the same naming pattern), this should work:
<?php
// define a more convenient variable for the separator
define('DS', DIRECTORY_SEPARATOR);
$pathToFiles = 'files folder';
$pathToDirs = 'subfolders';
// get a list of all .pdf files we're looking for
$files = glob($pathToFiles . DS . '*.pdf');
foreach ($files as $origPath) {
// get the name of the file from the current path and remove any trailing slashes
$file = trim(substr($origPath, strrpos($origPath, DS)), DS);
// get the folder-name from the filename, following the pattern "(Name, Number), word.pdf"
$folder = substr($file, 0, strrpos($file, ','));
// if a folder exists matching this file, move this file to that folder!
if (is_dir($pathToDirs . DS . $folder)) {
$newPath = $pathToDirs . DS . $folder . DS . $file;
rename($origPath, $newPath);
}
}

Categories