Home  >  Article  >  Backend Development  >  PHP function to create multi-layer (multi-level) directories

PHP function to create multi-layer (multi-level) directories

WBOY
WBOYOriginal
2016-07-25 09:00:08930browse
  1. /**

  2. *Create multi-level directories based on the path path
  3. *$dir target directory $mode permissions, 0700 represents the highest permissions
  4. */
  5. function makedir( $dir , $mode = "0700" ) {
  6. if(strpos($dir , "/" ) ){
  7. $dir_path = "" ;
  8. $dir_info = explode ( "/" , $dir );
  9. foreach($dir_info as $key => $value ){
  10. $dir_path .= $value ;
  11. if (! file_exists($dir_path )){
  12. @mkdir ( $dir_path , $mode ) or die ( "Failed to create folder" );
  13. @ chmod ( $dir_path , $mode );
  14. } else {
  15. $dir_path .= "/" ;
  16. continue ;
  17. }
  18. $dir_path .= "/" ;
  19. }
  20. return $dir_path ;
  21. } else {
  22. @mkdir( $dir , $mode ) or die( "Creation failed, please check permissions " );
  23. @chmod ( $dir , $mode );
  24. return $dir ;
  25. }
  26. } //end makedir

  27. //Call example

  28. makedir( "0/1/2 /3/" );
  29. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn