php遞迴建立和刪除檔案夾的代碼

來源:互聯網
上載者:User
  1. /**

  2. * 目錄產生類 :UtilsMakeDir
  3. * @author yepeng
  4. * @since 2010.3.18
  5. */
  6. class UtilsMakeDir{
  7. //基目錄 建立目錄時不會對這個目錄進行建立。這應該是個已經存在的目錄
  8. private static $makeBasePath = 'video';
  9. private static $delBasePath = 'video';

  10. /**

  11. * 遞迴建立目錄,
  12. * 建立成功返回這個全路徑,
  13. * 建立失敗返回false
  14. * @param String $pathString 路徑字串如'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. * 建立目錄,包括基目錄,比片要放在video(video為存在的目錄)下面,你傳入的參數應該是video/2/3/4
  41. * 建立成功返回這個全路徑,
  42. * 建立失敗返回false
  43. * @param String $pathString 路徑字串如'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. * 遞迴刪除
  66. * 刪除目錄及檔案
  67. * 如果傳一個‘video/2/3/4'這樣的路徑將刪除4下的所有目錄和檔案
  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. ?>

複製代碼

迴圈與遞迴,在winxp下測試成功,只要php檔案編碼為gb2312,檔案名稱隨意,應該把檔案名稱改為編碼為gb2312就可以了。

  1. deltree('./複件 複件 複件 複件 複件 複件 複件 複件 aaa');
  2. function deltree($pathdir)
  3. {
  4. //echo $pathdir.'
    ';//我調試時用的
  5. if(is_empty_dir($pathdir))//如果是空的
  6. {
  7. rmdir($pathdir);//直接刪除
  8. }
  9. else
  10. {//否則讀這個目錄,除了.和..外
  11. $d=dir($pathdir);
  12. while($a=$d->read()) //下只刪除$pathdir下
  13. {
  14. if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..'))
  15. {
  16. unlink($pathdir.'/'.$a); //如果是檔案就直接刪除
  17. }elseif(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..')) //如果是目錄
  18. {
  19. if(!is_empty_dir($pathdir.'/'.$a))//是否為空白
  20. {
  21. deltree($pathdir.'/'.$a); //如果不是,調用自身
  22. }else
  23. {
  24. rmdir($pathdir.'/'.$a); //如果是空就直接刪除
  25. }
  26. }
  27. }
  28. $d->close();
  29. //echo "必須先刪除目錄下的所有檔案";//我調試時用的
  30. rmdir($pathdir);
  31. }
  32. }
  33. function is_empty_dir($pathdir)
  34. {
  35. //判斷目錄是否為空白,我的方法不是很好吧?除了.和..之外有其他東西不是為空白
  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. ?>
複製代碼

方法二在winxp下測試成功,只要php檔案編碼為gb2312,檔案名稱隨意,應該把檔案名稱改為編碼為gb2312,就行,沒測。

  1. header("Content-Type:text/html; charset=gb2312");
  2. if(deleteDir('./複件 複件 複件 複件 複件 複件 複件 複件 複件 複件 複件 aaa'))
  3. echo "刪除成功";
  4. function deleteDir($dir)
  5. {
  6. if (@rmdir($dir)==false && is_dir($dir)) //刪除不了,進入刪除所有檔案
  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)) //是真實目錄
  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) //是目錄刪除不了
  30. return false;
  31. return true;
  32. }
  33. ?>
複製代碼

方法三、在winxp下測試成功,是列出目錄檔案 很好用。

  1. function listDir($dir)
  2. {
  3. static $break=0; if($break++==100) exit;//控制深入層數
  4. static $i=-0;
  5. if(is_dir($dir))//目錄
  6. {
  7. if ($dh = opendir($dir))//開啟
  8. {
  9. while (($file = readdir($dh)) !== false)
  10. {
  11. if((is_dir($dir."/".$file)) && $file!="." && $file!="..")//目錄
  12. {
  13. $j=$i;while($j--) echo "-------";
  14. echo "目錄名:".$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. //開始運行
  42. listDir(".");
  43. ?>
複製代碼

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.