Home > Article > Backend Development > How to use mkdir to create multi-level directories in PHP, _PHP tutorial
The example in this article describes how PHP uses mkdir to create a multi-level directory. Share it with everyone for your reference, the details are as follows:
Using mkdir() in PHP can create multi-level directories. Compared with the previous level-by-level creation, this function is very easy to use.
The following is the function introduction in the PHP manual:
Copy code The code is as follows: bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
The return value is of bool type.
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.
Note: You can create a Chinese directory Copy the code The code is as follows: mkdir(iconv("utf-8", "gbk", $path),0777,true); required Use iconv to transcode
The complete sample code is as follows:
<?php header("Content-type:text/html;charset=utf-8"); //要创建的多级目录 $path="dai/php/php学习"; //判断目录存在否,存在给出提示,不存在则创建目录 if (is_dir($path)){ echo "对不起!目录 " . $path . " 已经存在!"; }else{ //第三个参数是“true”表示能创建多级目录,iconv防止中文目录乱码 $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true); if ($res){ echo "目录 $path 创建成功"; }else{ echo "目录 $path 创建失败"; } } ?>
I hope this article will be helpful to everyone in PHP programming.