Hi I want to copy a file to all the folders in a directory
my root folder tree is
i want to copy file in root folder to every target folder
folder structure is
Hi I want to copy a file to all the folders in a directory
my root folder tree is
i want to copy file in root folder to every target folder
folder structure is
Since you have a static folder structure I'd suggest just storing the different subfolders in an array and loop through that to copy
the file
$rootpath = ".";
$destpaths = array("/Folder1/Subfolder1/AnotherSubFolder/Target/",
"/Folder2/Subfolder1/AnotherSubFolder/Target/",
"/Folder3/Subfolder1/AnotherSubFolder/Target/",
"/Folder4/Subfolder1/AnotherSubFolder/Target/",
"/Folder5/Subfolder1/AnotherSubFolder/Target/",
"/Folder6/Subfolder1/AnotherSubFolder/Target/",
"/FolderN/Subfolder1/AnotherSubFolder/Target/");
$file = '/example.txt';
$copyfrom = $rootpath . $file;
foreach($destpaths as $destpath)
{
$copyto = $rootpath . $destpath . $file;
if (!copy($copyfrom, $copyto)) {
echo "failed to copy $file";
}
}
As Aaron W. suggested and also used glob-php to get recursive paths(folder1,folder2...) i'm placing this code and file same folder and placed that in same root path as folder1,folder2...
$rootpath = "..";
$frompath = ".";
$file = '/example.txt';
$tosubpath="/Subfolder1/AnotherSubFolder/Target/";
$copyfrom = $frompath . $file;
listdirs($rootpath,$copyfrom,$tosubpath,$file);
function listdirs($dir,$copyfrom,$tosubpath,$file) {
$dirs = glob($dir . '/*', GLOB_ONLYDIR);
foreach($dirs as $destpath)
{
$copyto = $destpath. $tosubpath . $file;
if (!copy($copyfrom, $copyto)) {
echo "failed to copy ".$copyfrom." to".$copyto."<br>";
}else{
echo $copyfrom." copied to".$copyto."<br>";
}
}
}