How to use PHP function mkdir to create a multilevel directory _php tutorial

Source: Internet
Author: User
When we want to add a multilevel catalog to our website, we can use the PHP function mkdir parameters and descriptions

Path is required. Specifies the name of the directory to create.
Mode required. Set permissions. The default is 0777.
Recursive required. Specifies whether recursive mode is set.
Context required. Specifies the environment for file handles. Context is a set of options for modifying the behavior of a stream.

Description

PHP function mkdir Attempt to create a new directory specified by path.

The default mode is 0777, which means the maximum possible access rights.

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:

 
 
  1. function Mkdirs ($dir)
  2. {
  3. if (!is_dir ($dir))
  4. {
  5. if (!mkdirs (DirName ($dir))) {
  6. return false;
  7. }
  8. if (!mkdir ($dir, 0777)) {
  9. return false;
  10. }
  11. }
  12. return true;
  13. }
  14. Mkdirs (' div/css/layout ');
  15. In the same vein, PHP uses RmDir and unlink to recursively delete the multilevel directory code:
  16. function Rmdirs ($dir)
  17. {
  18. $ D = dir ($dir);
  19. while (False!== ($ Child = $d->Read ())) {
  20. if ($child! = '. ' && $child! = ' ... ') {
  21. if (Is_dir ($dir. '/'. $child))
  22. Rmdirs ($dir. '/'. $child);
  23. Else unlink ($dir. '/'. $child);
  24. }
  25. }
  26. $d- > Close ();
  27. RmDir ($dir);
  28. }

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


http://www.bkjia.com/PHPjc/446279.html www.bkjia.com true http://www.bkjia.com/PHPjc/446279.html techarticle when we want to add multi-level catalogs to our site, we can use PHP functions to mkdir parameters and describe the path required. Specifies the name of the directory to create. Mode required. Set permissions. The Silent ...

  • 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.