php建立目錄,刪除檔案夾及尋找不合法的目錄並刪除_PHP教程

來源:互聯網
上載者:User
php教程建立目錄,刪除檔案夾及尋找不合法的目錄並刪除
//$pattern = "/file-([0-2]d{3})-([1-9]d?)/"; 這個Regex,對檔案夾的月日進行捕獲,再進一步判斷處理

02 for($i =2005;$i<=2009;$i++)

03 {

04 for($j = 1;$j<30;$j++)

05 {

06 $dest_dir = "text/file-".$i."-$j";

07 if(!is_dir($dest_dir)) mkdir($dest_dir,0777);

08 }

09 }

10 //建立一些不滿足條件的檔案夾

11

12 for($t = 0;$t<10;$t++)

13 {

14 $dest_dir = "text/file-".$t;

15 if(!is_dir($dest_dir)) mkdir($dest_dir,0777);

16 }

17

18 //遍曆檔案夾處理 .

19 function listFile($dir)

20 {

21 $a = array();

22 $handle = opendir($dir);

23 while($file = readdir($handle))

24 {

25 handle_dir($dir,$file,&$a);

26 }

27

28 echo "the rege_array is ";

29 print("

"); 

30 print_r($a);

31 print("

");

32 }

33

34 function handle_dir($dir,$file,$a)

35 {

36

37 if($file == "." || $file == "..") return ;

38 $minYear = 2008;

39 $maxDay = 13;

40

41 $pattern = "/file-([0-2]d{3})-([1-9]d?)/";

42 $destPath = $dir."/".$file;

43

44 if(!is_dir($destPath))

45 {

46 //echo "$destPath is not a dir ;";

47 return;

48 }

49

50 //刪除不滿足格式的檔案

51 if(!preg_match($pattern,$file))

52 {

53 //echo "$fileis not the right rege. the program will unlink -- $destPath --
";

54 // unlink($destPath);

55 return;

56 }

57

58 preg_match_all($pattern,$file,$matchs);

59

60 //echo "$file is the right rege";

61 //print_r($matchs);

62 //echo "the year is ".$matchs[1][0]." and the day is ".$matchs[2][0]."
";

63 if(intval($matchs[1][0]) >$minYear && intval($matchs[2][0]) < $maxDay)

64 {

65 $a[]= $destPath;

66 }else { //不滿足條件的

67 //unlink($destPath);

68

69 }

70 }

71

72 listFile("text");


http://www.bkjia.com/PHPjc/444953.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444953.htmlTechArticlephp教程建立目錄,刪除檔案夾及尋找不合法的目錄並刪除 //$pattern = /file-([0-2]d{3})-([1-9]d?)/; 這個Regex,對檔案夾的月日進行捕獲,再...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.