Home > Backend Development > PHP Tutorial > PHP creates directories, deletes folders and finds illegal directories and deletes them_PHP tutorial

PHP creates directories, deletes folders and finds illegal directories and deletes them_PHP tutorial

WBOY
Release: 2016-07-20 11:07:32
Original
792 people have browsed it

PHP tutorial to create directories, delete folders and find illegal directories and delete them
//$pattern = "/file-([0-2]d{3})-([1-9]d ?)/"; This regular expression captures the month and day of the folder and then further judges and processes it

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 //Create some folders that do not meet the conditions

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 //Traverse the folder processing.

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("

"); </p>
<p>30 print_r($a); </p>
<p>31 print("
") ;

32 }

33

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

35 {

36

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

38 $minYear = 2008;

39 $maxDay = 13; >

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

43

44 if(!is_dir($destPath))

45 {

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

47 return;

48 }

49

50 //Delete files that do not meet the format

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,$ matches);

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 { //Does not meet the conditions

67 //unlink($destPath);

68 

69 } 

70 } 

71 

72 listFile("text");

http://www.bkjia.com/PHPjc/444953.html
www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/444953.htmlTechArticlephp tutorial to create directories, delete folders and find illegal directories and delete them //$pattern = /file- ([0-2]d{3})-([1-9]d?)/; This regular expression captures the month and day of the folder, and then...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template