Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/e6fbe4b40035afa2da6e0465437d16fa to your computer and use it in GitHub Desktop.
Save code-boxx/e6fbe4b40035afa2da6e0465437d16fa to your computer and use it in GitHub Desktop.
PHP Copy Entire Folder

PHP COPY ENTIRE FOLDER

https://code-boxx.com/copy-folder-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?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/");
<?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/");
<?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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment