PHP Recursive creation directory (pseudo original)

Source: Internet
Author: User
Tags bool comments locale log mkdir parent directory return safe mode

Sometimes you need to create a directory function recursively, and you need to use the DirName () function (get the directory portion of the path) and the mkdir () function (create the directory).

First, popularize the grammar:

DirName

(PHP 4, PHP 5)

dirname-returns the directory part of the path

Description?

String DirName (String $path)

Gives a string containing a full path to a file, which returns the name of the directory after the file name is removed.

Parameters?

path

A path.

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

return value?

Returns the parent directory of path. If path there is no slash in, return a point ('. ') ) that represents the current directory. Otherwise, the string that is removed from the path end of the/component (the last Slash and later) is returned.

Update log?

version Description
5.0.0 The operation of DirName () is binary safe starting from the PHP 5.0.0 version.
4.0.3 In this release, DirName () is modified to POSIX compliant.

Example?

Example #1 dirname () example

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

Comments?

Note:

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

Note:

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

Note:

Since PHP 4.3.0, you'll often get a slash or a dot back to dirname () in situations where the older functional Ity would have given you the empty string.

Check for examples of changes that occur below:


// PHP 4.3.0 以前
dirname('c:/'); // 返回 '.'

// PHP 4.3.0 以后
dirname('c:/x'); // 返回 'c:'
dirname('c:/Temp/x'); // 返回 'c:/Temp'
dirname('/x'); // 返回 '/' (or '\' on Windows)

?>

See?

  • BaseName ()-Returns the file name part of the path
  • PathInfo ()-Returns information about the file path
  • Realpath ()-Returns a normalized absolute path name

    Mkdir

    (PHP 4, PHP 5)

    mkdir-New Directory

    Description?

    BOOL MkDir (string$pathname[, int$mode= 0777 [, bool$recursive= False [, resource$context]]] )

    Attempt to create a new directory specified by pathname.

    Parameters?

    pathname

    The path to the directory.

mode

The default mode is 0777, which means the most likely access rights. For more information on mode, please read the chmod () page.

Note:

modeis ignored under Windows.

Note that you may want to specify a pattern with the octal number, which means that the number should begin with 0. The pattern is also modified by the current umask and can be changed using Umask ().

recursive

Allows the creation of nested directories specified in the pathname .

context

Note: Added support for context in PHP 5.0.0. For a description of the context (contexts) see Streams.

return value?

Return TRUE on success, or return on Failure FALSE .

Update log?

version Description
5.0.0 Add recursive parameters.
5.0.0 mkdir () can also be used for some URL encapsulation protocols. See the list of supported protocols and encapsulation protocols to see what URL encapsulation Protocols mkdir () supports.
4.2.0 modeBecome optional.

Example?

Example #1 mkdir () Example

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

Example #2 recursive use mkdir () with parameters

// Desired folder structure
$structure = './depth1/depth2/depth3/';

// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.

if (!mkdir($structure, 0, true)) {
die('Failed to create folders...');
}

// ...
?>

Comments?

Note: When Safe mode is enabled, PHP checks to see if the directory being scripted will have the same UID (owner) as the script being executed when the script is executed.

See?

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

      recursively create a 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" is true. 
      			return true;
      				else {//call mkdir () to create the "last directory, and" is false.
      		   return false;
      		   } else {//The file is already exist.
      	   return true; } 



      Reference:

      Click Open Link Click Open link
      Click Open link

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.