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

758
我在使用Bluehost安装WordPress时遇到了一些问题,因为上传文件夹wp-content/uploads不存在,导致我的WordPress主题出现错误。
显然,cPanel的WordPress安装程序没有创建这个文件夹,而HostGator则有。
因此,我需要在我的主题中添加代码来检查该文件夹是否存在,如果不存在则创建它。

17
如果('path/to/directory')目录不存在,就会创建一个名为'path/to/directory'的目录,并授予权限为0777。 - I am the Most Stupid Person
21个回答

-1

首先需要检查目录是否存在:file_exists('path_to_directory')

然后使用mkdir(path_to_directory)来创建目录。

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

更多关于mkdir()的信息请点击此处

完整代码如下:

$structure = './depth1/depth2/depth3/';
if (!file_exists($structure)) {
    mkdir($structure);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接