Home >Backend Development >PHP Tutorial >php mkdir creates multi-level directory example code_PHP tutorial
php mkdir can only create directories one level at a time. If there are multiple levels, we need to traverse recursively to create directories. Let me introduce to you some tips on how to use this function.
First introduce the mkdir() function
mkdir($path,0777,true);
The first parameter: required, represents the path of the multi-level directory to be created;
The second parameter: Set the permissions of the directory, the default is 0777, which means the maximum possible access rights;
The third parameter: true means allowing the creation of multi-level directories.
mkdir($dir,$mode); but it can only create one directory at a time, which means it cannot create multi-level directories at once, as follows
mkdir('aa'); //Only one aa directory can be created
mkdir('aa/bb/cc');//If there is an aa/bb directory, you can successfully create the cc directory, otherwise an error will be reported. If you want to create multiple directories, let’s look at the code below
Example code (supports creation of Chinese directories):
The code is as follows
|
Copy code
|
||||||||
header("Content-type:text/html;charset=utf-8"); //Multi-level directories to be created $path="dai/php/phplearning"; // Determine whether the directory exists and give a prompt if it exists. If it does not exist, create the directory if (is_dir($path)){ echo "Sorry! Directory " . $path . " already exists!"; }else{ //The third parameter is "true", which means that multi-level directories can be created. iconv prevents Chinese directories from being garbled $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true); if ($res){ echo "Directory $path created successfully"; }else{ echo "Failed to create directory $path"; } } ?> Look at another example of recursively creating a directory The editor has compiled two methods for creating directories recursively for your reference. Thank you!
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 Previous article:PHP session control cookie and Session processing_PHP tutorialNext article:PHP session control cookie and Session processing_PHP tutorial Related articlesSee more |