创建多级目录

写出一个能创建多级目录的PHP函数

 

 
 
  1. <?php  
  2. /*  
  3.     21、写出一个能创建多级目录的PHP函数。  
  4. *系统环境:windows/linux  
  5. *编译环境:php5/php4  
  6. *输入参数:存放在in.txt,多个参数时空格分隔  
  7.                     参数1是一个路径,用\或者/分隔  
  8.                     例如:www\b\c\d\e     d:\www\b\c\d\e   /var/www/html  var/../b/c  
  9.     输出:out.txt  
  10. */ 
  11. $params=getParams(1);  
  12. $dirParh=trim($params[0]);  
  13.  
  14. mkdirP($dirParh);  
  15.  
  16. function mkdirP($dirParh)  
  17. {  
  18.     $dirParh=str_replace("\\", "/", $dirParh);  
  19.     $paths=split("/",$dirParh);  
  20.     $nowPath="";  
  21.     if(substr($dirParh, 0,1)=="/")  
  22.     {  
  23.         $nowPath="/";  
  24.     }  
  25.     foreach($paths as $path)  
  26.     {  
  27.         if($path=="")  
  28.         {  
  29.             continue;  
  30.         }  
  31.         $nowPath.=$path;  
  32.         echo "$nowPath\n";  
  33.         //目录不存在  
  34.         if(!is_dir($nowPath))  
  35.         {  
  36.             echo "mkdir $nowPath\n";  
  37.             //建立目录失败  
  38.             if(!mkdir($nowPath))  
  39.                 return false;  
  40.         }  
  41.         $nowPath=rtrim($nowPath,"/");  
  42.         $nowPath.="/";  
  43.     }  
  44.     return true;  
  45. }  
  46.  
  47. /*  
  48.     从in.txt里读取参数  
  49. */ 
  50. function getParams($paramNum)  
  51. {  
  52.     $in=file_get_contents("in.txt");  
  53.     if($in===FALSE){  
  54.         error_msg("cannot read in.txt,please check in.txt exists\n");     
  55.     }  
  56.     $in=preg_replace("/(\s+)/i"" "$in);  
  57.     //多个参数时,按照空格分隔  
  58.     $parms=split(" ",trim($in));  
  59.     if($parms===FALSE)  
  60.     {  
  61.         error_msg("cannot get param from in.txt\n");  
  62.     }  
  63.     if(count($parms) < $paramNum)  
  64.     {  
  65.         error_msg("it needs $paramNum params\n");  
  66.     }  
  67.     return $parms;  
  68. }  
  69.  
  70. /*  
  71.     把结果输出到输出文件里  
  72.     当isClean=true时清空out.txt  
  73. */ 
  74. function output($msg,$isClean=false)  
  75. {  
  76.     if($isClean)  
  77.     {  
  78.     $handle = fopen('out.txt''w');  
  79.     fclose($handle);      
  80.     }  
  81.     error_log($msg."\n", 3, "out.txt");  
  82. }  
  83. /*  
  84.     输入错误信息  
  85.     如果$is_exit表示输入信息后退出  
  86. */ 
  87. function error_msg($msg,$is_exit=true)  
  88. {  
  89.     if($is_exit)  
  90.         die($msg."\n");  
  91.     else 
  92.         echo $msg."\n";  
  93. }  
  94. ?> 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值