您好,欢迎访问一九零五行业门户网

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() 例子
注释 ?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.
检查下面发生变化的例子:
参见 ?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()来改变。
recursiveallows the creation of nested directories specified in the pathname.
contextnote: 在 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() 例子
example #2 通过 recursive 参数使用 mkdir()
注释 ?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; } }
参考资料:
点击打开链接点击打开链接
点击打开链接
http://www.bkjia.com/phpjc/664278.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/664278.htmltecharticle有时候需要递归创建目录函数,这时需要使用dirname()函数(取得路径中的目录部分)和mkdir()函数(创建目录)。 先普及一下语法: dirnam...
其它类似信息

推荐信息