PHP mkdir Creating a multilevel Directory instance code _php tutorial

Source: Internet
Author: User
PHP mkdir Create a directory can only be created at the first level of the directory, if the multilevel we need to recursively create a directory, oh, let me show you some of the function of the use of the technique.

Let's introduce the function of mkdir ().

mkdir ($path, 0777,true);

First parameter: must, represents the path of the multilevel 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 indicates that a multilevel directory is allowed to be created.

mkdir ($dir, $mode); but it can only create one directory at a time, that is, it cannot create a multilevel directory at once, as follows

mkdir (' AA '); You can only create a single AA catalog.

mkdir (' aa/bb/cc ');//If you have a aa/bb directory, you can create the CC directory successfully or you will get an error. If you want to create multiple catalogs, let's look at the code below.

Example code (support for creating Chinese catalogs):

The code is as follows Copy Code


Header ("Content-type:text/html;charset=utf-8");

The multilevel directory to create

$path = "dai/php/php learning";

Determine if the directory exists no, there is a hint, does not exist to create the directory

if (Is_dir ($path)) {

echo "Sorry! Directory ". $path. "Already exists! ";

}else{

The third parameter is "true" to indicate that you can create a multilevel directory, iconv prevent Chinese directory garbled

$res =mkdir (Iconv ("UTF-8", "GBK", $path), 0777,true);

if ($res) {

echo "Catalog $path created successfully";

}else{

echo "Directory $path creation failed";

}

}


?>

Look at a recursive instance of creating a directory

Small compilation of two can be recursive to create a directory method for everyone to reference the study, thank you!

The code is as follows Copy Code

/*
*mkdir ($dir, $mode)
*php Creating a directory recursively
*/
function Mkdirs ($dir, $mode = 0777)
{
if (Is_dir ($dir) | | @mkdir ($DIR, $mode)) {
return true;
}
if (!mkdirs (DirName ($dir), $mode)) {
return false;
}
Return @mkdir ($dir, $mode);
}

function Mkdirs ($dir, $mode = 0777)
{
$dirArray = Explode ("/", $dir);
$dirArray = Array_filter ($dirArray);

$created = "";
foreach ($dirArray as $key = = $value) {
if (!empty ($created)) {
$created. = "/". $value;
if (!is_dir ($created)) {
mkdir ($created, $mode);
}
}else{
if (!is_dir ($value)) {
mkdir ($value, $mode);
}
$created. = $value;
}
}
}
?>
Code application Examples
$path = "abc/ff/ss/";
Mkdirs ($path, $mode = 0777);

Above is the PHP recursive creation of directories and multilevel directory content

http://www.bkjia.com/PHPjc/632720.html www.bkjia.com true http://www.bkjia.com/PHPjc/632720.html techarticle PHP mkdir Create a directory can only be created at the first level of the directory, if the multilevel we need to recursively create a directory, oh, let me show you some of the function of the use of the technique. Let's introduce a ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.