Copy contents of a directory to lots of other subdirectories - php

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 ?

Related

PHP Copy all files/folders from one directory to another

I'm trying to get a "reset" script working in PHP to essentially just copy all files/folders from one specific directory to another.
I've found plenty of snippets of code on Stackoverflow for recursively copying files/folders but I cannot get any of them to work.
My directory essentially looks like:
/public_html/folder1/reset.php
/public_html/folder1/files/
/public_html/docs/reset/files/
The reset.php file is the file I am trying to get working and I want to copy all of the files from /docs/reset/files/ over to /folder1/files/ overwriting everything...
I've tried the following code but it generates a 500mb~ file full of errors
<?php
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("../docs/reset/", "/")
?>
The code does appear to be cycling through correctly but the paths are all wrong.
[18-Jun-2020 07:54:33 UTC] PHP Warning: copy(/files//files/copies/index.php): failed to open stream: No such file or directory in /home/USERNAME/public_html/folder1/reset.php on line 11
If I could get the above working I was going to try the following:
$src = '/full/path/to/src'; // or relative path if so desired
$dst = '/full/path/to/dst'; // or relative path if so desired
$command = 'cp -a ' . $src . ' ' .$dst;
$shell_result_output = shell_exec(escapeshellcmd($command));
As I assume that would be faster?
I must be doing something wrong and I'm sure it's going to be something really minor but I cannot figure out what I am doing wrong.

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 *

How to delete all folders and files in a directory except a specific folder in PHP

How can I delete all folders and files except a specific folder?
uploaded->
. folder_A->
. . folder_A1 //empty folder
. . folder_A2 //full folder
. . img.png // a file
. .
. folder_B //empty
. .
. folder_c->
. . folder_c1 //empty folder
. . file.doc // a file
. .
I want remove all folders and file in it inside "uploaded" folder except a specific folder that I determined.
For example I want remove all folders and files except folder_c
you should try like this
function Delete($path)
{
if ((is_dir($path) === true) && ($path!='folder_c'))
{
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
Delete(realpath($path) . '/' . $file);
}
return rmdir($path);
}
else if (is_file($path) === true)
{
return unlink($path);
}
return false;
}
Hope this helps.

Copying files from multiple source to destination directories using PHP recursive copy function

The purpose of this question can be served by writing independent function for each source & destination directory in an include file but I'm looking for a better approach.
The following function copy files from one source directory to one destination directory.
How can I use this function to copy file from another source directory to destination directory?
Is array(); applicable here or explode(); shall be the right choice or none of these is applicable in this case?
if (isset($_POST['submit'])) {
$old_umask = umask(0);
if (!is_dir($dst)) mkdir($dst, 0777);
umask($old_umask);
function recurse_copy($src,$dst) {
$dir = opendir($src);
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);
//echo "$src";
}
$dir = $_POST['name'];
$src = "/home/user/public_html/directory/subdirectory/source/";
$dst = "/home/user/public_html/directory/subdirectory/destination/$dir/";
recurse_copy($src,$dst);
}

Copy folder without overwriting

I found this question (here) about copying a file without overwriting
How do you copy a file in PHP without overwriting an existing file?
What I need is a php script to copy all files in a folder, there are also some subfolders; so it should be recursive.
I need to transfer it via FTP so I don't know this makes a big difference to the approach.
Thanks a lot in advance!
Try this, from a comment on the manual page for copy:
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);
}
Note that this solution will happily overwrite any files that exist in the $dst directory. If you want to avoid that, you could wrap the code in this question into a function, and call that function instead of copy.
I'm not sure what you want to transfer via FTP, if you clarify that I'll be happy to edit my answer.
There is a big difference between filesystem copy and FTP copy you could take a look at the PEAR FTP Class

Categories