Jump to content

Recursive copy help!!


aximbigfan

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.