php-------Folder file copy and copy

In php development, files are often operated, folders and files are copied, copied, etc.

  1. /**
  2. * Folder file copy
  3. *
  4. * @param string $src source folder
  5. * @param string $dst destination folder
  6. * @return bool
  7. */
  8. function dir_copy($src = '', $dst = '')
  9. {
  10. if (empty($src) || empty($dst))
  11. {
  12. return false;
  13. }
  14. $ dir = open ( $ src );
  15. dir_mkdir ( $ dst );
  16. while (false !== ($file = readdir($dir)))
  17. {
  18. if (($file != '.') && ($file != '..'))
  19. {
  20. if (is_dir($src . '/' . $file))
  21. {
  22. dir_copy($src . '/' . $file, $dst . '/' . $file);
  23. }
  24. else
  25. {
  26. copy($src . '/' . $file, $dst . '/' . $file);
  27. }
  28. }
  29. }
  30. closedir($dir);
  31. return true;
  32. }
  33. /**
  34. * Create folder
  35. *
  36. * @param string $path folder path
  37. * @param int $mode access rights
  38. * @param bool $recursive whether to create recursively
  39. * @return bool
  40. */
  41. function dir_mkdir($path = '', $mode = 0777, $recursive = true)
  42. {
  43. clearstatcache();
  44. if (!is_dir($path))
  45. {
  46. mkdir($path, $mode, $recursive);
  47. return chmod($path, $mode);
  48. }
  49. return true;
  50. }

Related: php-------Folder file copy and copy