PHP Recursive creation directory (pseudo-original) _php tutorial

Source: Internet
Author: User
Tags locale
Sometimes you need to create a directory function recursively, you need to use the DirName () function (get the directory portion of the path) and the mkdir () function (create directory).

First, popularize the grammar:

DirName

(PHP 4, PHP 5)

dirname-returns the directory portion of the path

Description?

String DirName (String $path)

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

Parameters?

path

A path.

In Windows, slashes (/) and backslashes (\) can be used as directory separators. In other environments it is a slash (/).

return value?

Returns the parent directory of path. If path there are no slashes in, a point is returned ('. ') ), which represents the current directory. Otherwise, it returns the path string after the end of the/component (the last Slash and later) is removed.

Update log?

version Description
5.0.0 The operation of DirName () is binary safe starting with PHP version 5.0.0.
4.0.3 In this version, DirName () is modified to be 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 isn't aware of the actual filesystem, or path components suc H as "...".

Note:

dirname () is locale aware, so-it to see 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 has given you the empty string.

Check the following examples of changes:


// 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 portion of the path
  • PathInfo ()-Returns information about the file path
  • Realpath ()-Returns the 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]]] )

    Try creating a new directory that is specified by pathname.

    Parameters?

    pathname

    The path to the directory.

mode

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

Note:

modeis ignored under Windows.

Note that you might want to specify the pattern in octal numbers, which means that the number should begin with 0. The pattern is also modified by the current umask and can be changed with 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, see Streams.

return value?

Returns TRUE when successful, or on failure FALSE .

Update log?

version Description
5.0.0 Add recursive parameters.
5.0.0 mkdir () can also be used for certain URL encapsulation protocols. See the list of supported protocols and encapsulation protocols to see what URL encapsulation protocols are supported by mkdir ().
4.2.0 modeBecome an option.

Example?

Example #1 mkdir () Example

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

Example #2 recursive using 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 you enable Safe mode, PHP checks that the directory in which the script is executed will have the same UID (owner) as the script being executed when executing the script.

See?

  • Is_dir ()-Determines whether a given file name is a directory
  • RmDir ()-Delete directory

    To create a directory function recursively:

    /** * 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 FI Le is already exist.   return true;   }   }



    Resources:

    Click the Open link to open the link
    Click to open link

    http://www.bkjia.com/PHPjc/664278.html www.bkjia.com true http://www.bkjia.com/PHPjc/664278.html techarticle Sometimes you need to create a directory function recursively, you need to use the DirName () function (get the directory portion of the path) and the mkdir () function (create directory). First to popularize the grammar: Dirnam ...

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.