Home >Backend Development >PHP Tutorial >PHP recursively creates directories (pseudo-original)_PHP tutorial
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:
(PHP 4, PHP 5)
dirname — Returns the directory portion of the path
$path
)
Given a string containing the full path to a file, this function returns the directory name after removing the file name.
path
A path.
In Windows, both slash (/) and backslash () can be used as directory separators. In other circumstances it is a slash (/).
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).
版本 | 说明 |
---|---|
5.0.0 | dirname() 的操作从 PHP 5.0.0 版开始是二进制安全的。 |
4.0.3 | 在这个版本中,dirname() 被修正为 POSIX 兼容。 |
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>
?>
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>
?>
(PHP 4, PHP 5)
mkdir — Create a new directory
$pathname
[, int $mode
=
0777 [, bool $recursive
=
false [, resource$context
]]]
)
Try to create a new directory specified by pathname.
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.
Returns TRUE
on success, or FALSE
on failure.
Version | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|
5.0.0 | Add
|
||||||||
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 #1 mkdir() Example
<?php <br>
mkdir("/path/to/my/dir", 0700);<br>
?>
Example #2 Using mkdir()recursive
with
<?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>
?>
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.
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; } }
Click to open link Click to open link