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

Date Published: 21/04/2020 Published By: JaiSchool

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);

?>

Publish A Comment

Leave a Reply

Your email address will not be published. Required fields are marked *