Create a multilevel directory using PHP function mkdir

Source: Internet
Author: User

PHP default mkdir can only create one level of directory at a time, if you create a div/css/layout directory under the current directory, you need to create a div layer by layer, then create DIV/CSS and then create Div/css/layout. However, we hope that the program will help us to complete the process automatically.

In fact, the idea is very simple, 1. First determine whether the div directory exists, does not exist to create; 2. Determine if the subdirectory div/css exists, cannot exist, and 3. In the second step, the function itself is called recursively as a sub-directory. can also be in reverse order, 1. First determine whether the lowest-level directory div/css/layout exists, 2. Determine whether Div/css/layout's upper-level directory DIV/CSS exists, does not exist, and is recursive with DIV/CSS as a parameter.

Here is the program code for PHP function mkdir:

<?
function Mkdirs ($dir)
{
if (!is_dir ($dir))
{
if (!mkdirs (DirName ($dir)))
{return false;
}
if (!mkdir ($dir, 0777))
{
return false;
}
}
return true;
}
Mkdirs (' div/css/layout '); In the same vein, PHP uses RmDir and unlink to recursively delete the multilevel directory code:
function Rmdirs ($dir)
{$d = dir ($dir);
while (false!== ($child = $d->read ()))
{if ($child! = '. ' && $child! = ' ... ')
{if (Is_dir ($dir. '/'. $child)) Rmdirs ($dir. '/'. $child); else unlink ($dir. '/'. $child);
}
} $d->close (); RmDir ($dir);
}
?>


The above code example is the PHP function mkdir implementation to create a multilevel directory of the specific method.

Create a multilevel directory using PHP function mkdir

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.