Home > Backend Development > PHP Tutorial > PHP recursively creates directories (pseudo-original)_PHP tutorial

PHP recursively creates directories (pseudo-original)_PHP tutorial

WBOY
Release: 2016-07-13 10:42:18
Original
1016 people have browsed it

Sometimes you need to create a directory function recursively. In this case, you need to use the dirname() function (to obtain the directory part of the path) and the mkdir() function (to create the directory).

Let’s popularize the grammar first:

dirname

(PHP 4, PHP 5)

dirname — Returns the directory portion of the path

Description ?

string dirname ( string $path )

Given a string containing the full path to a file, this function returns the directory name after removing the file name.

Parameter ?

path

A path.

In Windows, both slash (/) and backslash () can be used as directory separators. In other circumstances it is a slash (/).

Return value ?

Returns the parent directory of path. If there are no slashes in path, a dot ('.') is returned, indicating the current directory. Otherwise, the string returned is the string after removing the /component at the end of path (the last slash and the following part).

Update log ?

版本 说明
5.0.0 dirname() 的操作从 PHP 5.0.0 版开始是二进制安全的。
4.0.3 在这个版本中,dirname() 被修正为 POSIX 兼容。

Example ?

Example #1 dirname() Example

<?php <br> echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc<br> echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or on Windows)<br> echo "3) " . dirname("."); // 3) .<br> ?>

Comment ?

Note:

dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

Note:

dirname() is locale aware, so for it to see the correct directory name with multibyte character paths, the matching locale must be set using the setlocale() function.

Note:

Since PHP 4.3.0, you will often get a slash or a dot back from dirname() in situations where the older functionality would have given you the empty string.

Check below an example of what happened:

<?php <br> <br> // PHP 4.3.0 以前<br> dirname('c:/'); // 返回 '.'<br> <br> // PHP 4.3.0 以后<br> dirname('c:/x'); // 返回 'c:'<br> dirname('c:/Temp/x'); // 返回 'c:/Temp'<br> dirname('/x'); // 返回 '/' (or '' on Windows)<br> <br> ?>

See ?

  • basename() - Return the file name part of the path
  • pathinfo() - Returns file path information
  • realpath() - Returns the normalized absolute pathname


    mkdir

    (PHP 4, PHP 5)

    mkdir — Create a new directory

    Description ?

    bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource$context ]]] )

    Try to create a new directory specified by pathname.

    Parameter ?

    pathname

    The path to the directory.

    mode

    The default mode is 0777, which means maximum possible access. For more information about mode please read the chmod() page.

    Note:

    mode in Ignored under Windows.

    Note that you may want to specify the mode as an octal number, which means the number should start with zero. The mode is also modified by the current umask, which can be changed with umask().

    recursive

    Allows the creation of nested directories specified in the pathname.

    context

    Note: Support for Context was added in PHP 5.0.0. See Streams for a description of context.

    Return value ?

    Returns TRUE on success, or FALSE on failure.

    Update log ?

    Version Description
    5.0.0 Add
    版本 说明
    5.0.0 添加 recursive 参数。
    5.0.0 mkdir() 也可用于某些 URL 封装协议。参见支持的协议和封装协议 的列表看看 mkdir() 支持哪些 URL 封装协议。
    4.2.0 mode 成为可选项。
    parameter.
    5.0.0 mkdir() also works with certain URLs Encapsulation protocol. See the list of supported protocols and encapsulated protocols to see what mkdir() supports. URL encapsulation protocol.
    4.2.0 becomes optional.

    Example ?

    Example #1 mkdir() Example

    <?php <br> mkdir("/path/to/my/dir", 0700);<br> ?>

    Example #2 Using mkdir()recursive with

    parameter <?php <br> // Desired folder structure<br> $structure = './depth1/depth2/depth3/';<br> <br> // To create the nested structure, the $recursive parameter <br> // to mkdir() must be specified.<br> <br> if (!mkdir($structure, 0, true)) {<br> die('Failed to create folders...');<br> }<br> <br> // ...<br> ?>

    Comment ?

    Note: When safe mode is enabled, PHP will check when executing a script whether the directory being scripted has the same UID (owner) as the script being executed.

    See ?

    • is_dir() - Determine whether the given file name is a directory
    • rmdir() - delete directory

      Recursively create directory function:

      /**
      	 * Create the directory recursively.
      	 * @param $path The directory to create, such as, /a/b/c/d/e/
      	 * @param $mode The mode of the directory to create, such as, 0755, 0777.
      	 */
      	function RecursiveMkdir($path,$mode) {
      		
      		if (!file_exists($path)) { // The file is not exist.
      			RecursiveMkdir(dirname($path), $mode); // Call itself.
      			if(mkdir($path, $mode)) { // Call mkdir() to create the last directory, and the result is true.
      				return true; 
      			} else { // Call mkdir() to create the last directory, and the result is false.
      				return false;
      		   }
             } else { // The file is already exist.
      		   return true;
      	   }
         }
      Copy after login



      References:

      Click to open link Click to open link
      Click to open link

      www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/664278.htmlTechArticleSometimes you need to create a directory function recursively. In this case, you need to use the dirname() function (get the directory part of the path) and mkdir() function (create directory). Let’s popularize the grammar first: dirnam...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template