Filesystem 函数
PHP Manual

mkdir

(PHP 4, PHP 5)

mkdir新建目录

说明

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

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

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

Note:

mode 在 Windows 下被忽略。自 PHP 4.2.0 起成为可选项。

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

Example #1 mkdir() 例子

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

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

Note: 自 PHP 5.0.0 rmdir() 也可用于某些 URL 封装协议。参见Supported Protocols and Wrappers 的列表看看 rmdir() 支持哪些 URL 封装协议。

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

Note: recursive 参数是 PHP 5.0.0 添加的。

Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。

参见 rmdir()

参数

pathname

The directory path.

mode

The mode is 0777 by default, which means the widest possible access. For more information on modes, read the details on the chmod() page.

Note:

mode is ignored on Windows.

Note that you probably want to specify the mode as an octal number, which means it should have a leading zero. The mode is also modified by the current umask, which you can change using 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 The recursive parameter was added
5.0.0 As of PHP 5.0.0 mkdir() can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers for a listing of which wrappers support mkdir()
4.2.0 The mode parameter became optional.

范例

Example #2 mkdir() example

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

Example #3 mkdir() using the recursive parameter

<?php
// Desired folder structure
$structure './depth1/depth2/depth3/';

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified.

if (!mkdir($structure0true)) {
    die(
'Failed to create folders...');
}

// ...
?>

注释

Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。

参见


Filesystem 函数
PHP Manual