PHP 8.4.1 Released!

mkdir

(PHP 4, PHP 5, PHP 7, PHP 8)

mkdir新建目录

说明

mkdir(
    string $directory,
    int $permissions = 0777,
    bool $recursive = false,
    ?resource $context = null
): bool

尝试新建由 directory 指定的目录。

参数

directory

目录的路径。

小技巧

如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen()。各种 wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。

permissions

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

注意:

permissions 在 Windows 下被忽略。

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

recursive

如果为 true,还将会创建指定 directory 的任何父级目录,并具有相同的权限。

context

上下文流(context stream) resource

返回值

成功时返回 true, 或者在失败时返回 false

注意:

如果创建的目录已存在,则视为错误,仍然返回 false。在尝试创建之前,使用 is_dir() 或者 file_exists() 检查目录是否已经存在。

错误/异常

目录已存在时,产生 E_WARNING 错误。

如果因为权限问题无法创建目录,导致 E_WARNING 错误。

示例

示例 #1 mkdir() 例子

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

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

<?php
// 期望的目录结构
$structure = './depth1/depth2/depth3/';

// 要创建嵌套结构,必须指定 mkdir()
// 的 $recursive 参数。

if (!mkdir($structure, 0777, true)) {
die(
'Failed to create directories...');
}

// ...
?>

参见

  • is_dir() - 判断给定文件名是否是一个目录
  • rmdir() - 删除目录
  • umask() - 改变当前的 umask

添加备注

用户贡献的备注 5 notes

up
35
jack dot sleight at gmail dot com
14 years ago
When using the recursive parameter bear in mind that if you're using chmod() after mkdir() to set the mode without it being modified by the value of uchar() you need to call chmod() on all created directories. ie:

<?php
mkdir
('/test1/test2', 0777, true);
chmod('/test1/test2', 0777);
?>

May result in "/test1/test2" having a mode of 0777 but "/test1" still having a mode of 0755 from the mkdir() call. You'd need to do:

<?php
mkdir
('/test1/test2', 0777, true);
chmod('/test1', 0777);
chmod('/test1/test2', 0777);
?>
up
19
aulbach at unter dot franken dot de
25 years ago
This is an annotation from Stig Bakken:

The mode on your directory is affected by your current umask. It will end
up having (<mkdir-mode> and (not <umask>)). If you want to create one
that is publicly readable, do something like this:

<?php
$oldumask
= umask(0);
mkdir('mydir', 0777); // or even 01777 so you get the sticky bit set
umask($oldumask);
?>
up
6
Protik Mukherjee
19 years ago
mkdir, file rw, permission related notes for Fedora 3////
If you are using Fedora 3 and are facing permission problems, better check if SElinux is enabled on ur system. It add an additional layer of security and as a result PHP cant write to the folder eventhough it has 777 permissions. It took me almost a week to deal with this!

If you are not sure google for SElinux or 'disabling SELinux' and it may be the cure! Best of luck!
up
3
julius - grantzau - c-o-m
13 years ago
Remember to use clearstatcache()

... when working with filesystem functions.

Otherwise, as an example, you can get an error creating a folder (using mkdir) just after deleting it (using rmdir).
up
-1
chelidze dot givia at gmail dot com
1 year ago
When creating a file using mkdir() the default root will be the DocumentRoot (in XAMPP) itself.

C:\xampp\htdocs\project/includes/something.php

If you use mkdir("myfile") in something.php, instead of creating the folder in includes, php will create it in the project folder
To Top