php Implementation Method of Establishing Multi level Directory

  • 2021-07-10 18:56:01
  • OfStack

This paper illustrates the implementation method of php in the form of an example. The code is simple, practical and powerful, which has a certain reference value for php programmers. Example details are as follows:


/**
 * Depending on the path path Create a multilevel directory 
 *$dir Target directory  $mode Authority, 0700 Indicates the highest permissions 
*/
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 ( " Failed to create folder " );
        @chmod ( $dir_path , $mode );
      } else {
        $dir_path .= "/" ;
 continue ;
 }
      $dir_path .= "/" ;
    }
    return $dir_path ;
  } else {
 @mkdir( $dir , $mode ) or die( " The establishment failed , Please check permissions " );
    @chmod ( $dir , $mode );
    return $dir ;
  }
} //end makedir
makedir( "0/1/2/3/" );

Related articles: