Skip to Content »

FlashApe » copy directory in php

 copy directory in php

  • April 20th, 2006
  • 7:02 am

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:

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

8 People had this to say...

Gravatar

Ya, permissions can be a pain. :)

[...] FlashApe » Blog Archive » 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: [...]

Gravatar
  • Sam Phippen
  • November 12th, 2006
  • 7:24 am

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

Gravatar
  • rich
  • November 12th, 2006
  • 11:17 am

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

Gravatar
  • Kees
  • December 10th, 2006
  • 1:02 pm

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

Gravatar
  • Anonymoose
  • March 12th, 2007
  • 3:53 pm

>

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.

Gravatar
  • Jeroen
  • November 28th, 2007
  • 4:16 pm

|| must be or

Gravatar

To Kees:

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

for example:
copyr ("images/fotos","images/fotos2");

Want your say?

* Required fields. Your e-mail address will not be published on this site


Fatal error: Call to undefined function: adsense_deluxe_ads() in /var/www/vhosts/visible-form.com/httpdocs/blog/wp-content/themes/rdc/comments.php on line 100