php循环删除,php 循环删除一个目录或清空一个目录

substr ($directory,-1) == '/')

24     {

25         $directory = substr ($directory,0,-1);

26     }

27

28     // if the path is not valid or is not a directory ...

29     if(! file_exists ($directory) || ! is_dir ($directory) || ! is_readable ($directory))

30     {

31         // ... we return -1 and exit the function

32         return -1;

33     }

34     // we open the directory

35     if($handle = opendir ($directory))

36     {

37         // and scan through the items inside

38         while(($file = readdir ($handle)) !== false)

39         {

40             // we build the new path

41             $path = $directory.'/'.$file;

42

43             // if the filepointer is not the current directory

44             // or the parent directory

45             if($file != '.' && $file != '..')

46             {

47                 // if the new path is a file

48                 if( is_file ($path))

49                 {

50                     // we add the filesize to the total size

51                     $size += filesize ($path);

52

53                 // if the new path is a directory

54                 }elseif( is_dir ($path))

55                 {

56                     // we call this function with the new path

57                     $handlesize = recursive_directory_size($path);

58

59                     // if the function returns more than zero

60                     if($handlesize >= 0)

61                     {

62                         // we add the result to the total size

63                         $size += $handlesize;

64

65                     // else we return -1 and exit the function

66                     }else{

67                         return -1;

68                     }

69                 }

70             }

71         }

72         // close the directory

73 closedir ($handle);

74     }

75     // if the format is set to human readable

76     if($format == TRUE)

77     {

78         // if the total size is bigger than 1 MB

79         if($size / 1048576 > 1)

80         {

81             return round ($size / 1048576, 1).' MB';

82

83         // if the total size is bigger than 1 KB

84         }elseif($size / 1024 > 1)

85         {

86             return round ($size / 1024, 1).' KB';

87

88         // else return the filesize in bytes

89         }else{

90             return round ($size, 1).' bytes'; 91         } 92     }else{ 93         // return the total filesize in bytes 94         return $size; 95     } 96 } 97 // ------------------------------------------------------------ 98 99 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值