• 技术文章 >后端开发 >php教程

    php创建目录,删除文件夹及查找不合法的目录并删除_PHP教程

    2016-07-20 11:07:32原创346
    php教程创建目录,删除文件夹及查找不合法的目录并删除
    //$pattern = "/file-([0-2]d{3})-([1-9]d?)/"; 这个正则表达式,对文件夹的月日进行捕获,再进一步判断处理

    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."//m.sbmmt.com/m/".$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");


    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444953.htmlTechArticlephp教程创建目录,删除文件夹及查找不合法的目录并删除 //$pattern = /file-([0-2]d{3})-([1-9]d?)/; 这个正则表达式,对文件夹的月日进行捕获,再...

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php 创建 目录 删除 文件夹 查找 不合法 php 教程 创建 目录 删除 文件夹 查找 不合法
    上一篇:php 中文与英文验证码程序代码_PHP教程 下一篇:LINUX 系统参数探测包括cpu,时间,内存等_PHP教程
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• PHP面试之常见基础算法(附代码示例)• php语言中使用json的技巧及json的实现代码详解_php实例• PHP的cURL库简介及使用示例_php实例• PHP中数据库单例模式的实现代码分享_php实例• 用PHP+MySQL搭建聊天室功能实例代码_php实例
    1/1

    PHP中文网