如果文件夹不存在,则创建一个文件夹 [英] Create a folder if it doesn't already exist

查看:43
本文介绍了如果文件夹不存在,则创建一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些使用 Bluehost 安装 WordPress 的情况,我遇到过我的 WordPress 主题出错,因为上传文件夹 wp-content/uploads 不存在.

I've run into a few cases with WordPress installs with Bluehost where I've encountered errors with my WordPress theme because the uploads folder wp-content/uploads was not present.

显然 Bluehost cPanel WordPress 安装程序不会创建此文件夹,尽管 HostGator 确实如此.

Apparently the Bluehost cPanel WordPress installer does not create this folder, though HostGator does.

所以我需要向我的主题添加代码来检查文件夹并以其他方式创建它.

So I need to add code to my theme that checks for the folder and creates it otherwise.

推荐答案

试试这个,使用 mkdir:

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

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

Note that 0777 is already the default mode for directories and may still be modified by the current umask.

这篇关于如果文件夹不存在,则创建一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆