Home >Backend Development >PHP Tutorial >Several functions for creating directories at unlimited levels in PHP
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);