3 Ways To Copy Entire Folder In PHP (Quick Examples)

Welcome to a quick tutorial and examples of how to copy an entire folder in PHP. Having trouble with copying files in PHP? Can’t find a native function that will copy entire folders?

To copy all the files inside a folder, a common solution is to manually get the list of files and copy them one by one.

  • $from = "D:/SOURCE/";
  • $to = "D:/DESTINATION/";
  • $files = array_filter(glob("$from*"), "is_file");
  • foreach ($files as $f) { copy($f, $to . basename($f)); }

That covers the basics, but how can we recursively copy the sub-folders too? Read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Copy Folder Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

PHP COPY ENTIRE FOLDER

All right, let us now get into the examples of copying an entire folder in PHP.

 

1) RECURSIVE COPY FOLDER USING GLOB

1-copy-glob.php
<?php
// (A) COPY ENTIRE FOLDER
function copyfolder ($from, $to, $ext="*") {
  // (A1) SOURCE FOLDER CHECK
  if (!is_dir($from)) { exit("$from does not exist"); }
 
  // (A2) CREATE DESTINATION FOLDER
  if (!is_dir($to)) {
    if (!mkdir($to)) { exit("Failed to create $to"); };
    echo "$to created\r\n";
  }
 
  // (A3) GET ALL FILES + FOLDERS IN SOURCE
  $all = glob("$from$ext", GLOB_MARK);
  print_r($all);
 
  // (A4) COPY FILES + RECURSIVE INTERNAL FOLDERS
  if (count($all)>0) { foreach ($all as $a) {
    $ff = basename($a); // CURRENT FILE/FOLDER
    if (is_dir($a)) {
      copyfolder("$from$ff/", "$to$ff/");
    } else {
      if (!copy($a, "$to$ff")) { exit("Error copying $a to $to$ff"); }
      echo "$a copied to $to$ff\r\n";
    }
  }}
}
 
// (B) GO!
copyfolder("C:/SOURCE/", "C:/TARGET/");

This is pretty much the “full version” of the snippet in the introduction. Not going to explain line-by-line, but the essentials are:

  • Get all the files and folders inside the source folder – $all = glob("$from$ext", GLOB_MARK);
  • Loop through all the files and folders – foreach ($all as $a) { ... }
  • If it is a folder, we call the function recursively to dig in – if (is_dir($a)) { copyfolder(SOURCE, DESTINATION); }
  • If it is a file, simply copy it to the destination folder – copy(SOURCE, DESTINATION);

This script will work just fine, but there is a potential problem with it. If the folder contains too many files, glob() can break the process with an “out-of-memory” error – This is where the next solution comes in handy.

 

 

2) RECURSIVE COPY FOLDER USING READDIR

2-copy-read.php
<?php
// (A) COPY ENTIRE FOLDER
function copyfolder ($from, $to) {
  // (A1) SOURCE FOLDER CHECK
  if (!is_dir($from)) { exit("$from does not exist"); }
 
  // (A2) CREATE DESTINATION FOLDER
  if (!is_dir($to)) {
    if (!mkdir($to)) { exit("Failed to create $to"); };
    echo "$to created\r\n";
  }
 
  // (A3) COPY FILES + RECURSIVE INTERNAL FOLDERS
  $dir = opendir($from);
  while (($ff = readdir($dir)) !== false) { if ($ff!="." && $ff!="..") {
    if (is_dir("$from$ff")) {
      copyfolder("$from$ff/", "$to$ff/");
    } else {
      if (!copy("$from$ff", "$to$ff")) { exit("Error copying $from$ff to $to$ff"); }
      echo "$from$ff copied to $to$ff\r\n";
    }
  }}
  closedir($dir);
}
 
// (B) GO!
copyfolder("C:/SOURCE/", "C:/TARGET/");

Look no further, this is the same recursive mechanic. The difference here is that we are using $ff = readdir($dir) to read the folders one by one – This is a safer alternative to deal with massive folders.

 

 

3) COPY FOLDER USING OS COMMAND

3-copy-cmd.php
<?php
// (A) SOURCE & TARGET
$from = "C:\\SOURCE";
$to = "C:\\TARGET";
 
// (B) COPY COMMAND
$os = strtolower(PHP_OS_FAMILY); // PHP 7.2 & ABOVE ONLY
$cmd = "";
if ($os == "windows") { $cmd = "Xcopy $from $to /E/H/C/I"; }
if ($os =="linux") { $cmd = "cp -R $from $to"; }
if ($cmd=="") { exit("Error copying"); }
 
// (C) RUN!
echo $cmd;
echo exec($cmd);

Lastly, here is the so-called “best alternative” – Just run a terminal command to copy the files, let the OS deal with it.

P.S. You need to give PHP permission to run the terminal command though.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

PHP Copy Entire Folder (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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