PHP recursive code to create and delete folders

Source: Internet
Author: User
  1. /**

  2. * Directory Generation class: Utilsmakedir
  3. * @author Yepeng
  4. * @since 2010.3.18
  5. */
  6. Class utilsmakedir{
  7. The directory is not established when the base directory is created. This should be a directory that already exists.
  8. private static $makeBasePath = ' video ';
  9. private static $delBasePath = ' video ';

  10. /**

  11. * Recursively create a directory,
  12. * Establish a successful return to this full path,
  13. * Build failed return False
  14. * @param string $pathString path string such as ' 2/3/4/5 '
  15. * @return False or string

  16. public static function MakeDir ($pathString) {

  17. $pathArray = explode ('/', $pathString);
  18. if (empty ($pathArray [0])) {
  19. return false;
  20. }
  21. $path = Array_shift ($pathArray);
  22. Self:: $basePath = self:: $basePath. ' /'. $path;
  23. if (Is_dir (self:: $basePath)) {
  24. $path = implode ('/', $pathArray);
  25. Self::makedir ($path);
  26. }
  27. else{
  28. @mkdir (self:: $basePath, 0777);
  29. $path = implode ('/', $pathArray);
  30. Self::makedir ($path);
  31. }
  32. if (Is_dir (self:: $basePath)) {
  33. Return self:: $basePath;
  34. }
  35. else{
  36. return false;
  37. }
  38. } */
  39. /**
  40. * Set up the directory, including the base directory, the chip to be placed in the video (video for the existence of the directory), your incoming parameters should be VIDEO/2/3/4
  41. * Establish a successful return to this full path,
  42. * Build failed return False
  43. * @param string $pathString path string such as ' VIDEO/2/3/4/5 '
  44. * @return False or string
  45. **/
  46. public static function MakeDir ($pathString) {
  47. $pathArray = explode ('/', $pathString);
  48. $tmpPath = Array_shift ($pathArray);
  49. foreach ($pathArray as $val) {
  50. $tmpPath. = "/". $val;
  51. if (Is_dir ($tmpPath)) {
  52. Continue
  53. }
  54. else {
  55. @mkdir ($tmpPath, 0777);
  56. }
  57. }
  58. if (Is_dir ($tmpPath)) {
  59. return $tmpPath;
  60. }
  61. else{
  62. return false;
  63. }
  64. } /**
  65. * Recursive deletion
  66. * Delete directories and files
  67. * If you pass a path such as ' VIDEO/2/3/4 ', all directories and files under 4 will be deleted
  68. * @param string $stringPath
  69. */
  70. public static function Deldir ($stringPath) {
  71. if (! $handle = @opendir ($stringPath)) {
  72. return false;
  73. }
  74. while (false!== ($file = Readdir ($handle))) {
  75. if ($file! = '. ' && $file! = ' ... ') {
  76. $tmpdir = $stringPath. " /". $file;
  77. if (Is_dir ($tmpdir)) {
  78. Self::d eldir ($tmpdir);
  79. RmDir ($tmpdir);
  80. }
  81. if (Is_file ($tmpdir)) {
  82. Unlink ($tmpdir);
  83. }
  84. }
  85. }
  86. Closedir ($handle);
  87. }}
  88. ?>

Copy Code

Loop and recursion, in WinXP Test success, as long as the PHP file encoded as gb2312, file name arbitrary, should be changed to encode the file name gb2312 on it.

  1. deltree ('./copy copy of duplicate copy of duplicate copy of duplicate copy of the re-piece of AAA ');
  2. function deltree ($pathdir)
  3. {
  4. echo $pathdir. '
    ';//I use it for debugging
  5. if (Is_empty_dir ($pathdir))//If it is empty
  6. {
  7. RmDir ($pathdir);//delete directly
  8. }
  9. Else
  10. {//otherwise read this directory except for. and. Outside
  11. $d =dir ($pathdir);
  12. while ($a = $d->read ())//under Delete only $pathdir
  13. {
  14. if (Is_file ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... '))
  15. {
  16. Unlink ($pathdir. ' /'. $a); If it's a file, delete it directly.
  17. }elseif (Is_dir ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... ')) If the directory
  18. {
  19. if (!is_empty_dir ($pathdir. ' /'. $a))//Is empty
  20. {
  21. deltree ($pathdir. ' /'. $a); If not, call itself
  22. }else
  23. {
  24. RmDir ($pathdir. ' /'. $a); If it is empty, delete it directly.
  25. }
  26. }
  27. }
  28. $d->close ();
  29. echo "must first delete all files in the directory";//I use it for debugging
  30. RmDir ($pathdir);
  31. }
  32. }
  33. function Is_empty_dir ($pathdir)
  34. {
  35. I'm not good at judging if the directory is empty, right? besides. and. Something else is not empty.
  36. $d =opendir ($pathdir);
  37. $i = 0;
  38. while ($a =readdir ($d))
  39. {
  40. $i + +;
  41. }
  42. Closedir ($d);
  43. if ($i >2) {return false;}
  44. else return true;
  45. }
  46. ?>
Copy Code

Method Two in WinXP test success, as long as the PHP file encoded as gb2312, the file name is arbitrary, should be changed to encode the filename gb2312, on line, not measured.

    1. !--? php header ("content-type:text/html; charset=gb2312 ");
    2. if (Deletedir ('./copy-copy-copy-copy-copy-duplicate-copy-copy-duplicate-copy-re---------")
    3. echo "Delete succeeded";
    4. function Deletedir ($dir)
    5. {
    6. if (@rmdir ($dir) ==false && is_dir ($dir))//delete, go to delete all files
    7. {
    8. if ($DP = Opendir ($ dir))
    9. {
    10. while (($file =readdir ($DP)) = False)
    11. {
    12. if ($file! = '. ' && $file! = '. .')
    13. {//echo $file = $dir. '/'. $file; Echo '
      ';
    14. $file = $dir. '/'. $file;
    15. if (Is_dir ($file))//is the real directory
    16. {
    17. deletedir ($file);
    18. }else {
    19. unlink ($file);
    20. }
    21. }
    22. }
    23. Closedir ($DP);
    24. }else
    25. {
    26. return false;
    27. }
    28. }
    29. if (Is_dir ($dir) && @rmdir ($dir) ==false)//directory cannot be deleted
    30. return false;
    31. return true;
    32. }
    33. ?
Copy Code

Method Three, under WinXP test success, is to list the directory file is very useful.

  1. function Listdir ($dir)
  2. {
  3. static $break = 0; if ($break ++==100) exit;//control depth of layer
  4. static $i =-0;
  5. if (Is_dir ($dir))//directory
  6. {
  7. if ($dh = Opendir ($dir))//Open
  8. {
  9. while (($file = Readdir ($DH))!== false)
  10. {
  11. if (Is_dir ($dir. " /". $file)) && $file! =". && $file! = "...") Directory
  12. {
  13. $j = $i, while ($j-) echo "-------";
  14. echo " directory Name:". $dir. " /". $file."
    ";
  15. $i + +;
  16. Listdir ($dir. " /". $file);
  17. $i--;
  18. }
  19. Else
  20. {
  21. if ($file! = "." && $file! = "...")
  22. {
  23. $j = $i, while ($j-) echo "-------";
  24. $ext =trim (Extend ($file));
  25. if ($ext = = ' jpg ')
  26. echo $dir. '/'. $file. "
    ";
  27. }
  28. }
  29. }
  30. Closedir ($DH);
  31. }
  32. }
  33. }
  34. function extend ($file _name)
  35. {
  36. $retval = "";
  37. $pt =strrpos ($file _name, ".");
  38. if ($pt) $retval =substr ($file _name, $pt +1, strlen ($file _name)-$pt);
  39. return ($retval);
  40. }
  41. Start running
  42. Listdir (".");
  43. ?>
Copy Code

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.