Home >Backend Development >PHP Tutorial >Several functions for creating directories at unlimited levels in PHP

Several functions for creating directories at unlimited levels in PHP

高洛峰
高洛峰Original
2016-11-29 15:53:111170browse

Creating a directory is something we often encounter when uploading files. If I want to generate a relative directory based on the date and save the file, I need this function. The example code is as follows:

function mkdirs($dir)

{

if(!is_dir($dir)){

if(!mkdirs(dirname($dir))){

return false;}

if(!mkdir($dir,0777)){

return false;}

}

return true;

}

//Test method

$img_path = realpath("../../../upfile/www..php.cn/") .'/'.date("y/m/d/");

mkdirs($img_path);

//function

function mkdir_r($dirname, $rights=0777){

$dirs = explode('/', $dirname);

$dir='';

foreach ($dirs as $part) {

as $part.'/';

if (!is_dir($ dir) && strlen($dir)>0)

                                                                                                                                                                                 );

mkdir_r($path);

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