百科狗-知识改变命运!
--

mkdir() - 新建目录 - php 文件目录函数

是丫丫呀12个月前 (11-21)阅读数 14#技术干货
文章标签目录

mkdir()

(PHP 4, PHP 5, PHP 7)

新建目录

说明

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

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

参数

$pathname

目录的路径。

$mode

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

Note:

mkdir() - 新建目录 - php 文件目录函数

$mode在 Windows 下被忽略。

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

$recursive

允许递归创建由$pathname所指定的多级嵌套目录。

$context

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

返回值

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

更新日志

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

范例

Example #1mkdir()例子

通过$recursive参数使用mkdir()

注释

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

参见

  • is_dir() 判断给定文件名是否是一个目录
  • rmdir() 删除目录
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:
 
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: 
This is an annotation from Stig Bakken:
The mode on your directory is affected by your current umask. It will end
up having ( and (not )). If you want to create one
that is publicly readable, do something like this: 
Don't forget to use the recursive option on Windows if creating a folder below a folder that already exists. Otherwise you may get a very confusing error message, "Warning: mkdir(): No such file or directory in d:\path\to\file" where you think the folder should exist but you forgot to create the parent(s).
For example, if d:\path exists but there is nothing in it: 
Somehow the recursive version of mkdir didn't work for me on Mac and the workaraounds listed below alsow didn't work for me, so heres my solution:

Tested and works ;)
Please note that in a shared environment I failed to take into account an existing umask when I did a mkdir(dirname, 0755). This ended up creating the directory (function returned true), but I didn't have rights to do anything inside the folder, nor could I even view that it existed via ftp.
However, file_exists(dirname) returned true. Eventually I figured out what happened and was able to rmdir(dirname), then created the directory correctly.
So, when writing scripts you expect to be portable, either use umask to set your umask accordingly, or do a straight mkdir(dirname) followed by chmod(dirname, 0755) (or whatever it is you're looking for). If you make the same mistake I did, you should be able to rmdir() or chmod() the folder so it's accessible.
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!
If you have problems with the SAFE MODE Restriction in effect i.e. if you try to create and access to subdirectorys recursive you can use ftp-lib like this.

Maybe it helps.
Toppi
One small correction on a note from Frank in June 2006 on recursive directories under Windows.
First - this should be in the documentation as its the only function that I know of that php does not fix the slashes automatically.
Franks note stated:

This doesn't work in windows:
 
----
This will work a bit better :) 
When I created folder on windows with mkdir, I found some problem from folder nam so I write this function.

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)