[Code] Copy A Directory (Including Files and Folders Inside It) At Another Location in PHP

Recursive program to copy a directory***

<?php

function copy_folder($ffc,$path)
{
	$dir = opendir($ffc);
	if(!file_exists($path))
	{
		mkdir($path);
	}

	while(($data = readdir($dir)) != false)
	{
		if($data != "." && $data != "..")
		{
			if(is_dir($ffc."/".$data))
			{
				copy_folder($ffc."/".$data,$path."/".$data);
			}
			else{
				copy($ffc."/".$data,$path."/".$data);
			}
		}
	}
	closedir($dir);
}

$folder_for_copy ="source_directory";
$path = "destination";
copy_folder($folder_for_copy,$path);

?>

Leave a Comment