copy directory in php

April 20, 200625 Comments

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. }

Filed in: php

About the Author ()

Comments (25)

Trackback URL | Comments RSS Feed

  1. Ya, permissions can be a pain. :)

  2. Sam Phippen says:

    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

  3. rich says:

    sam – the error was just telling you left a dot in there on line 41 after changing the code.

  4. Kees says:

    Hi,
    I only know a little PHP… Where should I put the source and destination folder (file)
    Thanks!

  5. Anonymoose says:

    >

    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.

  6. To Kees:

    Destination (and source) directory in:
    copyr(source, destination)

    for example:
    copyr (“images/fotos”,”images/fotos2″);

  7. veighlyn says:

    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 ” “;
    }

    ?>

  8. veighlyn says:

    <?

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

    ?>

  9. veighlyn says:

    sorry,
    more of the code:

  10. Kimbhoot says:

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

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

  11. rich says:

    they’re supposed to be quotation marks, the post has been updated to fix that.

  12. Jerick says:

    Thanks a lot for the code..my codes finaaly works…GodBless

  13. Aziz Light says:

    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…

  14. JSB says:

    That’s true, all files and dirs have 777 after copy.

  15. This is very helpful, thanks, now i can continue working in creating DIR in php

  16. itguy51 says:

    What does “Unexpected T_IF on line 12″ mean?

  17. Ben says:

    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?

  18. jehyson says:

    very nice function!! thanks

  19. eyal says:

    nice thank you man!

  20. 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

Back to Top