PHP

HOW TO COPY ENTIRE FOLDER IN PHP

GLOB COPY FOLDER

01

function gcopy ($from, $to) {   CREATE FOLDER   if (!is_dir($to)) { mkdir($to); }      RECURSIVE COPY   $all = glob("$from*", GLOB_MARK);   foreach ($all as $a) {     $ff = basename($a);     if (is_dir($a))       { gcopy("$from$ff/", "$to$ff/"); }     else { copy($a, "$to$ff"); }   } }

READDIR COPY FOLDER

02

function rcopy ($from, $to) {   CREATE FOLDER   if (!is_dir($to)) { mkdir($to); }     RECURSIVE COPY   $dir = opendir($from);   while (($ff = readdir($dir)) !== false) {    if ($ff!="." && $ff!="..") {     if (is_dir("$from$ff"))       { rcopy("$from$ff/", "$to$ff/"); }     else { copy("$from$ff", "$to$ff"); }   }}   closedir($dir); }

COMMAND LINE COPY

03

TERMINAL COMMAND $os = strtolower(PHP_OS_FAMILY); $cmd = ""; if ($os == "windows") { $cmd = "Xcopy $from $to /E/H/C/I"; }  if ($os =="linux") { $cmd = "cp -R $from $to"; }   RUN COMMAND if ($cmd!="") { exec($cmd); }