Home  >  Article  >  Backend Development  >  PHP code to create and delete folders recursively

PHP code to create and delete folders recursively

WBOY
WBOYOriginal
2016-07-25 09:08:13929browse
  1. /**

  2. * Directory generation class: UtilsMakeDir
  3. * @author yepeng
  4. * @since 2010.3.18
  5. */
  6. class UtilsMakeDir{
  7. //base directory This directory will not be created when creating the directory. This should be an existing directory
  8. private static $makeBasePath = 'video';
  9. private static $delBasePath = 'video';

  10. /**

  11. * Create the directory recursively,
  12. * Return the full path if the creation is successful,
  13. * Return false if the creation fails
  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. * Create a directory, including the base directory. For example, if the picture is to be placed under video (video is the existing directory), the parameter you pass in should be video/2/3/4
  41. * If the creation is successful, the full path will be returned,
  42. * Create Failure returns 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 like '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::delDir($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, under winxp The test is successful. As long as the PHP file is encoded as gb2312 and the file name is arbitrary, the file name should be changed to gb2312.

  1. deltree('./copy copy copy copy copy copy copy copy copy aaa');
  2. function deltree($pathdir)
  3. {
  4. //echo $pathdir.'
    if(is_empty_dir($pathdir))//If it is empty
  5. {
  6. rmdir($pathdir);//Delete it directly
  7. }
  8. else
  9. {//Otherwise read this directory , except . and ..
  10. $d=dir($pathdir);
  11. while($a=$d->read()) //Only delete $pathdir next
  12. {
  13. if(is_file($pathdir. '/'.$a) && ($a!='.') && ($a!='..'))
  14. {
  15. unlink($pathdir.'/'.$a); //If it is a file Just delete it directly
  16. }elseif(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..')) //If it is a directory
  17. {
  18. if (!is_empty_dir($pathdir.'/'.$a))//Is it empty
  19. {
  20. deltree($pathdir.'/'.$a); //If not, call itself
  21. }else
  22. {
  23. rmdir ($pathdir.'/'.$a); //If it is empty, delete it directly
  24. }
  25. }
  26. }
  27. $d->close();
  28. //echo "All files in the directory must be deleted first" ;//What I use when debugging is
  29. rmdir($pathdir);
  30. }
  31. }
  32. function is_empty_dir($pathdir)
  33. {
  34. //My method of judging whether the directory is empty is not very good, right? Except for. and.. There is something else that is not empty
  35. $d=opendir($pathdir);
  36. $i=0;
  37. while($a=readdir($d))
  38. {
  39. $i++;
  40. }
  41. closedir($d) ;
  42. if($i>2){return false;}
  43. else return true;
  44. }
  45. ?>
Copy code

Method 2 The test was successful under winxp. As long as the PHP file is encoded as gb2312 and the file name is arbitrary, the file name should be changed to gb2312 and it will be fine. No test.

  1. header("Content-Type: text/html; charset=gb2312");
  2. if(deleteDir('./ copy copy copy copy copy copy copy copy copy copy copy copy aaa') )
  3. echo "Delete successfully";
  4. function deleteDir($dir)
  5. {
  6. if (@rmdir($dir)==false && is_dir($dir)) //Cannot 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) //The directory cannot be deleted
  30. return false;
  31. return true;
  32. }
  33. ?>
Copy code

method 3. The test was successful under winxp. It is very useful to list directory files.

  1. function listDir($dir)
  2. {
  3. static $break=0; if($break++==100) exit;//Control the number of depth levels
  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



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn