如果文件夹不存在,则创建一个文件夹

新手上路,请多包涵

我遇到过一些使用 Bluehost 安装 WordPress 的案例,我的 WordPress 主题出现错误,因为上传文件夹 wp-content/uploads 不存在。

显然,Bluehost cPanel WordPress 安装程序不会创建此文件夹,但 HostGator 会。

所以我需要将代码添加到我的主题中,以检查文件夹并以其他方式创建它。

原文由 Scott B 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 450
2 个回答

试试这个,使用 mkdir

 if (!file_exists('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);
}

请注意, 0777 已经是目录的默认模式,可能仍会被当前的 umask 修改。

原文由 Gumbo 发布,翻译遵循 CC BY-SA 4.0 许可协议

我们可以使用 mkdir 创建文件夹。我们也可以为它设置权限。

>  Value Permission
> 0     cannot read, write or execute
> 1     can only execute
> 2     can only write
> 3     can write and execute
> 4     can only read
> 5     can read and execute
> 6     can read and write
> 7     can read, write and execute
>
> ```

<?PHP

// Making a directory with the provision // of all permissions to the owner and // the owner’s user group mkdir(“/documents/post/”, 0770, true)

?>

”`

原文由 Ashok kumawat 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题