Add files and directory to a specific sub directory - php

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.

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));

Adding all subdirectories to a zip archive

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 *

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);
?>

Zip file add and rename with php

I have this piece of code..,everything works fine ,but an issue with renaming
$zip = new ZipArchive();
$zipPath = 'images/userfiles/'.$company_details->company_name.'_products.zip';
$emptydir = $company_details->company_name.'_product_logos';
if ($zip->open($zipPath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
$new_filename = substr($my_file, strrpos($my_file, '/') + 1);
$zip->addFile($my_file, $new_filename);
$zip->addEmptyDir($emptydir);
foreach($parts_list as $pl) {
if(!empty($pl['part_image'])){
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $baseurl . '/images/group-logo/' . $pl['part_image'])) {
$img_file = 'images/group-logo/' . $pl['part_image'];
$new_filename2 = substr($img_file, strrpos($img_file, '/') + 1);
$zip->addFile($img_file,$emptydir . '/' . $new_filename2);
/*********the problem here******
Is there any way to rename the file that i added on the previous line to something else ,already tried zip rename and renameindex but not working
for example i want to rename the file $new_filename2 to $new_filename2.'something' with out affecting the original file's name
P.S the files to be renamed are inside another folder in the zip
*******************************/
}
}
}
$zip->close();
}
Since you do not want to effect the original file I would think that you are going to need to include a copy statement. Something like;
$file = '$new_filename2';
$newfile = '$new_filename2.zip';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
Okay i figured it out
Zip creates a lock on the file..you cant rename on the fly ,close it and rename again
$zip->addFile('.....');
....
$zip->close();
and open zip again and rename

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