Home  >  Article  >  Backend Development  >  How to detect and create directories in a php loop

How to detect and create directories in a php loop

WBOY
WBOYOriginal
2016-07-25 09:04:491073browse
  1. // Loop to create directories
  2. function mk_dir($dir, $mode = 0755)
  3. {
  4. if (is_dir($dir) || @mkdir($dir,$mode)) return true;
  5. if (!mk_dir(dirname($dir),$mode)) return false;
  6. return @mkdir($dir,$mode);
  7. }
Copy code

Method 2:

  1. $filepath = "test/upload/2010/image.gif";

  2. createDir(dirname($filepath));
  3. //Then you can move_uploaded_file !

  4. /*

  5. * Function: Loop to detect and create folders
  6. * Parameters: $path Folder path
  7. * Return:
  8. */
  9. function createDir($path){
  10. if (! file_exists($path)){
  11. createDir(dirname($path));
  12. mkdir($path, 0777);
  13. }
  14. }
  15. ?>

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