<?php
	function copydir($source, $destination, $chmod = 0755, $recurse = false)
	{
		if(!is_dir($destination))
		{
			$oldumask = umask(0); 
			mkdir($destination, $chmod); // so you get the sticky bit set 
			umask($oldumask);
		}

		$dir_handle = opendir($source) or die("Unable to open $source");
		while ($file = readdir($dir_handle)) 
		{
			if($file != "." && $file != ".." && !is_dir("$source/$file"))
				copy("$source/$file", "$destination/$file");
			elseif($recurse && is_dir("$source/$file") && $file != "." && $file != "..")
			{	/////////// sublevel 1 /////////////
				$source1 = "$source/$file";
				$destination1 = "$destination/$file";
				if(!is_dir($destination1))
				{
					$oldumask = umask(0); 
					mkdir($destination1, $chmod); // so you get the sticky bit set 
					umask($oldumask);
				}
				$dir_handle1 = opendir($source1) or die("Unable to open $source1");
				while ($file1 = readdir($dir_handle1)) 
				{
					if($file1 != "." && $file1 != ".." && !is_dir("$source1/$file1"))
					{
						$result = copy("$source1/$file1", "$destination1/$file1");
						if (!$result)
							echo "Copy failed: source = $source1/$file1 --- destination = $destination1/$file1<br>";
					}
					elseif(is_dir("$source1/$file1") && $file1 != "." && $file1 != "..")
					{	/////////// sublevel 2 /////////////
						$source2 = "$source1/$file1";
						$destination2 = "$destination1/$file1";
						if(!is_dir($destination2))
						{
							$oldumask = umask(0); 
							mkdir($destination2, $chmod); // so you get the sticky bit set 
							umask($oldumask);
						}
						$dir_handle2 = opendir($source2) or die("Unable to open $source2");
						while ($file2 = readdir($dir_handle2)) 
						{
							if($file2 != "." && $file2 != ".." && !is_dir("$source2/$file2"))
							{
								$result = copy("$source2/$file2", "$destination2/$file2");
								if (!$result)
									echo "Copy failed: source = $source2/$file2 --- destination = $destination2/$file2<br>";
							}
							elseif(is_dir("$source2/$file2") && $file2 != "." && $file2 != "..")
							{	/////////// sublevel 3 /////////////
								$source3 = "$source2/$file2";
								$destination3 = "$destination2/$file2";
								if(!is_dir($destination3))
								{
									$oldumask = umask(0); 
									mkdir($destination3, $chmod); // so you get the sticky bit set 
									umask($oldumask);
								}
								$dir_handle3 = opendir($source3) or die("Unable to open $source3");
								while ($file3 = readdir($dir_handle3)) 
								{
									if($file3 != "." && $file3 != ".." && !is_dir("$source3/$file3"))
									{
										$result = copy("$source3/$file3", "$destination3/$file3");
										if (!$result)
											echo "Copy failed: source = $source3/$file3 --- destination = $destination3/$file3<br>";
									}
									elseif(is_dir("$source3/$file3") && $file3 != "." && $file3 != "..")
									{	/////////// sublevel 4 /////////////
										$source4 = "$source3/$file3";
										$destination4 = "$destination3/$file3";
										if(!is_dir($destination4))
										{
											$oldumask = umask(0); 
											mkdir($destination4, $chmod); // so you get the sticky bit set 
											umask($oldumask);
										}
										$dir_handle4 = opendir($source4) or die("Unable to open $source4");
										while ($file4 = readdir($dir_handle4)) 
										{
											if($file4 != "." && $file4 != ".." && !is_dir("$source4/$file4"))
											{
												$result = copy("$source4/$file4", "$destination4/$file4");
												if (!$result)
													echo "Copy failed: source = $source4/$file4 --- destination = $destination4/$file4<br>";
											}
											elseif(is_dir("$source4/$file4") && $file4 != "." && $file4 != "..")
											{	/////////// sublevel 5 /////////////
												$source5 = "$source4/$file4";
												$destination5 = "$destination4/$file4";
												if(!is_dir($destination5))
												{
													$oldumask = umask(0); 
													mkdir($destination5, $chmod); // so you get the sticky bit set 
													umask($oldumask);
												}
												$dir_handle5 = opendir($source5) or die("Unable to open $source5");
												while ($file5 = readdir($dir_handle5)) 
												{
													if($file5 != "." && $file5 != ".." && !is_dir("$source5/$file5"))
													{
														$result = copy("$source5/$file5", "$destination5/$file5");
														if (!$result)
															echo "Copy failed: source = $source5/$file5 --- destination = $destination5/$file5<br>";
													}
													elseif(is_dir("$source5/$file5") && $file5 != "." && $file5 != "..")
													{	/////////// sublevel 6 /////////////
														$source6 = "$source5/$file5";
														$destination6 = "$destination5/$file5";
														if(!is_dir($destination6))
														{
															$oldumask = umask(0); 
															mkdir($destination6, $chmod); // so you get the sticky bit set 
															umask($oldumask);
														}
														$dir_handle6 = opendir($source6) or die("Unable to open $source6");
														while ($file6 = readdir($dir_handle6)) 
														{
															if($file6 != "." && $file6 != ".." && !is_dir("$source6/$file6"))
															{
																$result = copy("$source6/$file6", "$destination6/$file6");
																if (!$result)
																	echo "Copy failed: source = $source6/$file6 --- destination = $destination6/$file6<br>";
															}
															elseif(is_dir("$source6/$file6") && $file6 != "." && $file6 != "..")
															{	/////////// sublevel 7 /////////////
																$source7 = "$source6/$file6";
																$destination7 = "$destination6/$file6";
																if(!is_dir($destination7))
																{
																	$oldumask = umask(0); 
																	mkdir($destination7, $chmod); // so you get the sticky bit set 
																	umask($oldumask);
																}
																$dir_handle7 = opendir($source7) or die("Unable to open $source7");
																while ($file7 = readdir($dir_handle7)) 
																{
																	if($file7 != "." && $file7 != ".." && !is_dir("$source7/$file7"))
																	{
																		$result = copy("$source7/$file7", "$destination7/$file7");
																		if (!$result)
																			echo "Copy failed: source = $source7/$file7 --- destination = $destination7/$file7<br>";
																	}
																	elseif(is_dir("$source7/$file7") && $file7 != "." && $file7 != "..")
																	{	/////////// sublevel 8 /////////////
																		$source8 = "$source7/$file7";
																		$destination8 = "$destination7/$file7";
																		if(!is_dir($destination8))
																		{
																			$oldumask = umask(0); 
																			mkdir($destination8, $chmod); // so you get the sticky bit set 
																			umask($oldumask);
																		}
																		$dir_handle8 = opendir($source8) or die("Unable to open $source8");
																		while ($file8 = readdir($dir_handle8)) 
																		{
																			if($file8 != "." && $file8 != ".." && !is_dir("$source8/$file8"))
																			{
																				$result = copy("$source8/$file8", "$destination8/$file8");
																				if (!$result)
																					echo "Copy failed: source = $source8/$file8 --- destination = $destination8/$file8<br>";
																			}
																			elseif(is_dir("$source8/$file8") && $file8 != "." && $file8 != "..")
																			{	/////////// sublevel 9 /////////////
																				$source9 = "$source8/$file8";
																				$destination9 = "$destination8/$file8";
																				if(!is_dir($destination9))
																				{
																					$oldumask = umask(0); 
																					mkdir($destination9, $chmod); // so you get the sticky bit set 
																					umask($oldumask);
																				}
																				$dir_handle9 = opendir($source9) or die("Unable to open $source9");
																				while ($file9 = readdir($dir_handle9)) 
																				{
																					if($file9 != "." && $file9 != ".." && !is_dir("$source9/$file9"))
																					{
																						$result = copy("$source9/$file9", "$destination9/$file9");
																						if (!$result)
																							echo "Copy failed: source = $source9/$file9 --- destination = $destination9/$file9<br>";
																					}
																					elseif(is_dir("$source9/$file9") && $file9 != "." && $file9 != "..")
																					{	/////////// sublevel 10 /////////////
																						$source10 = "$source9/$file9";
																						$destination10 = "$destination9/$file9";
																						if(!is_dir($destination10))
																						{
																							$oldumask = umask(0); 
																							mkdir($destination10, $chmod); // so you get the sticky bit set 
																							umask($oldumask);
																						}
																						$dir_handle10 = opendir($source10) or die("Unable to open $source10");
																						while ($file10 = readdir($dir_handle10)) 
																						{
																							if($file10 != "." && $file10 != ".." && !is_dir("$source10/$file10"))
																							{
																								$result = copy("$source10/$file10", "$destination10/$file10");
																								if (!$result)
																									echo "Copy failed: source = $source10/$file10 --- destination = $destination10/$file10<br>";
																							}
																							elseif(is_dir("$source10/$file10") && $file10 != "." && $file10 != "..")
																							{	/////////// sublevel 11 /////////////
																								echo "Found 11th nested level.  Need to add another level to this module. Contact Silver Dolphin Solutions, LLC.<br>";
																							}
																						}
																						closedir($dir_handle10);
																					}
																				}
																				closedir($dir_handle9);
																			}
																		}
																		closedir($dir_handle8);
																	}	/////////// sublevel 8 /////////////
																}
																closedir($dir_handle7);
															}	/////////// sublevel 7 /////////////
														}
														closedir($dir_handle6);
													}	/////////// sublevel 6 /////////////
												}
												closedir($dir_handle5);
											}	/////////// sublevel 5 /////////////
										}
										closedir($dir_handle4);
									}	/////////// sublevel 4 /////////////
								}
								closedir($dir_handle3);
							}	/////////// sublevel 3 /////////////
						}
						closedir($dir_handle2);
					}	/////////// sublevel 2 /////////////
				}
				closedir($dir_handle1);
			}	/////////// sublevel 1 /////////////
		}
		closedir($dir_handle);
	}
?>