Jump to content

Recursive copy


aximbigfan

Recommended Posts

I have been struggling with filesystem stuff for a bit now.

 

I am trying to make a function to copy one dir or file, to another dir.

 

like this

                      source            target

recurse_copy("/test1/test2", "/testa/testb");

 

My current code is this, but it is making the complete dir structure leading up to the dir I want to copy, in the target.

 

private function recurse_copy($source, $target)
{
	$s2 = substr($source, 0, strrpos($source, '/'));

	if (is_dir($source))
		$dir_handle = opendir($source);
	mkdir($target . '/' . $s2, 0777, true);
	while ($file = readdir($dir_handle))
	{
		if ($file!="." && $file!="..")
		{
			if (!is_dir($source . '/' . $file))
				copy($source . '/' . $file, $target . '/' . $s2 . '/' . $file);
			else
				$this->recurse_copy($source . '/' . $file, $target);
		}
	}
	closedir($dir_handle);
	return true;
}

 

Thanks,

Chris

Link to comment
https://forums.phpfreaks.com/topic/124502-recursive-copy/
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.