copy directory in php

While trying to figure out how to copy an entire directory to a new location (on the same web server) in php, I came across this function. I took that as a base and modified it to handle premissons better:

  1. function copyr($source, $dest){
  2. // Simple copy for a file
  3. if (is_file($source)) {
  4. $c = copy($source, $dest);
  5. chmod($dest, 0777);
  6. return $c;
  7. }
  8.  
  9. // Make destination directory
  10. if (!is_dir($dest)) {
  11. $oldumask = umask(0);
  12. mkdir($dest, 0777);
  13. umask($oldumask);
  14. }
  15.  
  16. // Loop through the folder
  17. $dir = dir($source);
  18. while (false !== $entry = $dir->read()) {
  19. // Skip pointers
  20. if ($entry == "." || $entry == "..") {
  21. continue;
  22. }
  23.  
  24. // Deep copy directories
  25. if ($dest !== "$source/$entry") {
  26. copyr("$source/$entry", "$dest/$entry");
  27. }
  28. }
  29.  
  30. // Clean up
  31. $dir->close();
  32. return true;
  33. }

25 thoughts on “copy directory in php”

  1. I got this error when directly copying the code and then changing it a little
    Parse error: syntax error, unexpected ‘.’ in /home/.helmut/samphippen/radical-mod.et3x.net/yourspace/builder.php on line 41

    i changed it so that instead of using a function with variables i used a solid source directory and a $_post variable for the destination

  2. >

    If you get the unexpected ‘.’ error, you may have copy-and-pasted alternate, “prettier” characters for single and double quotes which the parser doesn’t recognise.

    The problem will likely go away if you replace them with ‘ and ” respectively.

  3. Question:
    I am writing an admin code,
    where upon adding a user a directory is created based on the users name.
    I also want to have a dummy directory with source files copied to the new directory when the user is created..
    Stuck on the copy portion.
    Here’s what I have:
    <?

    if (($action==”Add”) || ($action == “Modify”) || ($act==”Edit”)) {
    print ” “;
    print ” “;
    print ” “;
    }
    else {
    print ” “;
    }

    ?>

  4. <?

    if (($action==”Add”) || ($action == “Modify”) || ($act==”Edit”)) {
    print ” “;
    print ” “;
    print ” “;
    }
    else {
    print ” “;
    }

    ?>

  5. in lines 20, 25 and 26 what are those characters?

    ‘.’ || $entry == ‘..’

  6. Thanks for the code. It works pretty well (the copying part) however permissions don’t work. All the created files and folders have 777 permissions…

  7. For some reason i am getting this error.

    Parse error: syntax error, unexpected T_STRING in /home/voting/public_html/web_install.php on line 87

    87. copyr(“$source/$entry”, “$dest/$entry”);

    This is my callto the function
    copyr (“01_sample/student_pics”,”02_sample”);

    Any ideas why this is happening?

  8. 1. Line 26 still has some character encoding errors because of “curly quotes” instead of “straight quotes.” That is the error Ben was having. Delete the “” characters to fix the problem.

    2. You don’t need to use umask on the directory. You can use chmod just like you did for files.

    3. If you want to preserve the original permissions instead of setting all destination files to 777 (like Aziz Light and JSB said), change line 5 (and the command for directories) to:
    chmod ($dest, fileperms ($source) );

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>