搜索
首页php教程php手册php中mkdir函数递归创建目录(文件夹)

在php中mkdir函数是创建目录它自身是不可以递归创建目录了,我们需要遍历进行目录然后调用mkdir即可,下面来看一下操作方法。

<?php
/**
 * Makes directory and returns BOOL(TRUE) if exists OR made.
 *
 * @param  $path Path name
 * @return bool
 */
function rmkdir($path, $mode = 0755) {
    $path = rtrim(preg_replace(array(
        "/\/",
        "//{2,}/"
    ) , "/", $path) , "/");
    $e = explode("/", ltrim($path, "/"));
    if (substr($path, 0, 1) == "/") {
        $e[0] = "/" . $e[0];
    }
    $c = count($e);
    $cp = $e[0];
    for ($i = 1; $i < $c; $i++) {
        if (!is_dir($cp) && !@mkdir($cp, $mode)) {
            return false;
        }
        $cp.= "/" . $e[$i];
    }
    return @mkdir($path, $mode);
}
?>

例2

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:

<?php
function mkdir_r($dirName, $rights = 0777) {
    $dirs = explode(&#39;/&#39;, $dirName);
    $dir = &#39;&#39;;
    foreach ($dirs as $part) {
        $dir.= $part . &#39;/&#39;;
        if (!is_dir($dir) && strlen($dir) > 0) mkdir($dir, $rights);
    }
}
?>

Tested and works ;)


例3

<?php
function mkdirs($dir) {
    if (!is_dir($dir)) {
        if (!mkdirs(dirname($dir))) {
            return false;
        }
        if (!mkdir($dir, 0777)) {
            return false;
        }
    }
    return true;
}
function rmdirs($dir) {
    $d = dir($dir);
    while (false !== ($child = $d->read())) {
        if ($child != &#39;.&#39; && $child != &#39;..&#39;) {
            if (is_dir($dir . &#39;/&#39; . $child)) rmdirs($dir . &#39;/&#39; . $child);
            else unlink($dir . &#39;/&#39; . $child);
        }
    }
    $d->close();
    rmdir($dir);
}
?>

三个创建目录的函数都有自己的优点,这里我也没有一个个测试了,只使用了第二个感觉非常的不错。


永久链接:

转载随意!带上文章地址吧。

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器