搜尋
首頁php教程php手册PHP递归创建目录(伪原创)

有时候需要递归创建目录函数,这时需要使用dirname()函数(取得路径中的目录部分)和mkdir()函数(创建目录)。

先普及一下语法:

dirname

(PHP 4, PHP 5)

dirname — 返回路径中的目录部分

说明 ?

string dirname ( string $path )

给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名。

参数 ?

path

一个路径。

在 Windows 中,斜线(/)和反斜线(\)都可以用作目录分隔符。在其它环境下是斜线(/)。

返回值 ?

返回 path 的父目录。 如果在 path 中没有斜线,则返回一个点('.'),表示当前目录。否则返回的是把 path 中结尾的 /component(最后一个斜线以及后面部分)去掉之后的字符串。

更新日志 ?

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

范例 ?

Example #1 dirname() 例子

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

检查下面发生变化的例子:

<?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> ?>

参见 ?

  • basename() - 返回路径中的文件名部分
  • pathinfo() - 返回文件路径的信息
  • realpath() - 返回规范化的绝对路径名

    mkdir

    (PHP 4, PHP 5)

    mkdir — 新建目录

    说明 ?

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

    尝试新建一个由 pathname 指定的目录。

    参数 ?

    pathname

    目录的路径。

    mode

    默认的 mode 是 0777,意味着最大可能的访问权。有关 mode 的更多信息请阅读 chmod() 页面。

    Note:

    mode 在 Windows 下被忽略。

    注意也许想用八进制数指定模式,也就是说该数应以零打头。模式也会被当前的 umask 修改,可以用 umask()来改变。

    recursive

    Allows the creation of nested directories specified in the pathname.

    context

    Note: 在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。

    返回值 ?

    成功时返回 TRUE, 或者在失败时返回 FALSE

    更新日志 ?

    版本 说明
    5.0.0 添加 recursive 参数。
    5.0.0 mkdir() 也可用于某些 URL 封装协议。参见支持的协议和封装协议 的列表看看 mkdir() 支持哪些 URL 封装协议。
    4.2.0 mode 成为可选项。

    范例 ?

    Example #1 mkdir() 例子

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

    Example #2 通过 recursive 参数使用 mkdir()

    <?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: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。

    参见 ?

    • is_dir() - 判断给定文件名是否是一个目录
    • rmdir() - 删除目录

      递归创建目录函数:

      /**
      	 * 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;
      	   }
         }



      参考资料:

      点击打开链接点击打开链接
      点击打开链接



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具