aximbigfan Posted September 22, 2008 Share Posted September 22, 2008 Hi, I have the following code. function recurse_copy($source, $target) { if (is_dir($source)) $dir_handle = opendir($source); $dirname = substr($source, strrpos($source, '/')+1); mkdir($target.'/'.$dirname,0777); while($file=readdir($dir_handle)) { if ($file!='.' && $file!='..') { if (!is_dir($source.'/'.$file)) copy($source.'/'.$file, $target.'/'.$dirname.'/'.$file); else { $target1 = $target.'/'.$dirname; $this->recurse_copy($source.'/'.$file, $target1); } } } closedir($dir_handle); return true; } The function works somewhat, BUT... It isn't copying the source dir to the new dir, it is copying the CONTENTS of the source dir, to the target dir. if I were to do this: recurse_copy("/opt/", "/opt2"); Lets assume that in /opt/ is /1/2/3/4 Now, after the above code is executed, the structure of /opt2/ will look like this: /opt2/1/2/3/4 What I would like it to look like is: /opt2/opt/1/2/3/4 Is there any way to solve this? I have a mental block when it comes to working with loops like this... Thanks! Chris Link to comment https://forums.phpfreaks.com/topic/125236-recursive-copy-help/ Share on other sites More sharing options...
Garethp Posted September 22, 2008 Share Posted September 22, 2008 recurse_copy("/opt/", "/opt2/opt"); Link to comment https://forums.phpfreaks.com/topic/125236-recursive-copy-help/#findComment-647401 Share on other sites More sharing options...
aximbigfan Posted September 22, 2008 Author Share Posted September 22, 2008 Huh... Well... Drat... That was... Simple... Thanks! Chris Link to comment https://forums.phpfreaks.com/topic/125236-recursive-copy-help/#findComment-647422 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.