I have the following function which is supposed to help copy folders and files inside folders and files. I would like to copy the following source :
Documents/Projects/Sensitization Workshop Meeting/Milestones/Activity No 1/Tasks/Task One/New File.txt
To the following destination :
Documents/Projects/Sensitization Workshop Meeting/Milestones/Asqwerty/Tasks/Task One/ New File.txt
So the folder changing is Activity No 1 -> Asqwerty , I tried the following function but it didn't work :
// copy recursive
function recursive_copy($source, $dest) {
if (is_dir($source)) {
$dir_handle = opendir($source);
while ($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (is_dir($source . "/" . $file)) {
if (!is_dir($dest . "/" . $file)) {
mkdir($dest . "/" . $file, 0777, TRUE);
}
$this->recurse_copy($source . '/' . $file, $dest . '/' . $file);
} else {
copy($source . "/" . $file, $dest . "/" . $file);
}
}
}
closedir($dir_handle);
} else {
copy($source, $dest);
}
}
But it only copies to the following level :
Source : Documents/Projects/Sensitization Workshop Meeting/Milestones/Activity No 1/Tasks
Destination : Documents/Projects/Sensitization Workshop Meeting/Milestones/Asqwerty/Tasks
How can I sought this out ?
To create folders and files resources when a user logs into the system usually use
contant.php
define('DIR_WRITE_MODE', 0777);
define('BASE_PRINCIPAL', 'resources/base_theme_member/web2');
define('MEMBER_RESOURCES', '/resources/');
controller
$source = BASE_PRINCIPAL;
$dest = MEMBER_PRINCIPAL . $user_id . MEMBER_RESOURCES;
private function _create_basic_themplates( $user_id, $source, $dest ){
// recursive function to copy
// all subdirectories and contents:
mkdir($dest, DIR_WRITE_MODE, TRUE);
foreach(
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item
){
if( $item->isDir()){
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName(), DIR_WRITE_MODE, TRUE);
}
else{
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
}
I have not tested this but I think this should work:
function recursive_copy($source, $dest)
{
if(is_dir($source)))
{
if(!is_dir($dest))
{
mkdir($dest, 0777, true);
}
$dir_items = array_diff(scandir($source), array('..', '.'));
if(count($dir_items) > 0)
{
foreach($dir_items as $v)
{
$this->recursive_copy(rtrim(rtrim($source, '/'), '\\').DIRECTORY_SEPARATOR.$v, rtrim(rtrim($dest, '/'), '\\').DIRECTORY_SEPARATOR.$v);
}
}
}
elseif(is_file($source)))
{
copy($source, $dest);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.