2014-09-05
有时候需要递归创建目录函数,这时需要使用dirname()函数(取得路径中的目录部分)和mkdir()函数(创建目录)。
先普及一下语法:
(PHP 4, PHP 5)
dirname ― 返回路径中的目录部分
$path
)给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名。
path
一个路径。
在 Windows 中,斜线(/)和反斜线(/)都可以用作目录分隔符。在其它环境下是斜线(/)。
返回 path 的父目录。 如果在 path
中没有斜线,则返回一个点('.'),表示当前目录。否则返回的是把 path
中结尾的 /component(最后一个斜线以及后面部分)去掉之后的字符串。
版本 | 说明 |
---|---|
5.0.0 | dirname() 的操作从 PHP 5.0.0 版开始是二进制安全的。 |
4.0.3 | 在这个版本中,dirname() 被修正为 POSIX 兼容。 |
Example #1 dirname() 例子
echo "1) " . dirname("/etc/passwd") . PHP_EOL; // 1) /etc
echo "2) " . dirname("/etc/") . PHP_EOL; // 2) / (or / on Windows)
echo "3) " . dirname("."); // 3) .
?>
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 4.3.0 以前
dirname('c:/'); // 返回 '.'
// PHP 4.3.0 以后
dirname('c:/x'); // 返回 'c:'
dirname('c:/Temp/x'); // 返回 'c:/Temp'
dirname('/x'); // 返回 '/' (or '/' on Windows)
?>
(PHP 4, PHP 5)
mkdir ― 新建目录
$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() 例子
mkdir("/path/to/my/dir", 0700);
?>
Example #2 通过 recursive
参数使用 mkdir()
// 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...');
}
// ...
?>
Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。
递归创建目录函数:
/** * 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; } }
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
6
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
7
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
8
常用dos命令及语法
09-27
将视频设置为Android手机开机动画的教程
2014-12-11
PHP中include和require区别之我见
2014-09-05
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
学ug编程如何快速入门?
2022-03-17
java中的info是什么意思
2022-03-24
PHP数组函数array
2014-09-05
零基础的初学者怎样学习java,或者应该先学什么?
2022-03-21
猫猫地铁逃生游戏下载v2.0.0 安卓单机版
其它手游 132.24MB
下载弹壳特攻队破解版内置MOD菜单最新版下载v4.2.1 安卓无敌版
其它手游 763.71MB
下载弹壳特攻队国际服最新版(Survivor.io)下载v4.2.1安卓版
其它手游 763.71MB
下载弹壳特攻队官方正版下载v4.2.1 安卓最新版
其它手游 763.71MB
下载智勇大闯关游戏下载v1.0.3 安卓版
其它手游 110.7MB
下载智勇大闯关手机版下载v1.0.3 安卓版
其它手游 110.7MB
下载智勇大闯关最新版下载v1.0.3 安卓版
其它手游 110.7MB
下载滚动球球大乱斗游戏下载v1.0.4 安卓版
其它手游 154.73MB
下载快乐小鸡动物园最新版下载v1.8 安卓版
下载
快乐小鸡3最新版下载v1.8 安卓版
下载
英雄大行动游戏下载v1.1.1 安卓版
下载
滑草大冒险纳西妲游戏(grassskiing)下载v1.1.1 安卓最新版
下载
最强脑洞大师游戏下载v3.2.19 安卓版
下载
脑洞大师海外版下载v3.2.19 安卓版
下载
脑洞大师2手机版下载v3.2.19 安卓版
下载
小小脑洞大师最新版下载v3.2.19 安卓版
下载