php实现建立多层级目录的方法

yipeiwu_com5年前PHP代码库

本文以实例形式说明了php建立多层级目录的实现方法,代码简洁实用,功能强大,对于php程序员来说有一定的参考借鉴价值。实例详情如下:

/**
 *根据路径path建立多级目录
 *$dir目标目录 $mode权限,0700表示最高权限
*/
function makedir( $dir , $mode = "0700" ) {
  if(strpos($dir , "/" )){
    $dir_path = "" ;
    $dir_info = explode ( "/" , $dir );
    foreach($dir_info  as  $key => $value ){
      $dir_path .= $value ;
      if (!file_exists($dir_path )){
        @mkdir ( $dir_path , $mode ) or  die ( "建立文件夹时失败了" );
        @chmod ( $dir_path , $mode );
      } else {
        $dir_path .= "/" ;
 continue ;
 }
      $dir_path .= "/" ;
    }
    return $dir_path ;
  } else {
 @mkdir( $dir , $mode ) or die( "建立失败了,请检查权限" );
    @chmod ( $dir , $mode );
    return $dir ;
  }
} //end makedir
makedir( "0/1/2/3/" );

相关文章

php 多线程上下文中安全写文件实现代码

复制代码 代码如下: <?php /** * @usage: used to offer safe file write operation in multiple threads...

WordPress中"无法将上传的文件移动至"错误的解决方法

今天在网页上传图片到博客,结果提示:“无法将上传的文件移动至 /home/wwwroot/wp-content/uploads/2013/”,郁闷了,认为是权限问题,修改了文件,都改成了...

php实现的生成排列算法示例

本文实例讲述了php实现的生成排列算法。分享给大家供大家参考,具体如下: <?php function perm($s, $n, $index) { if($n =...

PHP中使用CURL伪造来路抓取页面或文件

复制代码 代码如下: // 初始化 $curl = curl_init(); // 要访问的网址 curl_setopt($curl, CURLOPT_URL, 'http://asen...

PHP的静态方法与普通方法用法实例分析

PHP的静态方法与普通方法用法实例分析

本文实例讲述了PHP的静态方法与普通方法用法。分享给大家供大家参考,具体如下: 代码 <?php class TestClass { public $attri...