我在Bluehost上安装WordPress时遇到过一些情况,我的WordPress主题遇到了错误,因为上传文件夹wp-content/uploads不存在。
显然Bluehost cPanel WordPress安装程序不会创建这个文件夹,但HostGator可以。
因此,我需要向我的主题添加代码,以检查文件夹并创建它。
我在Bluehost上安装WordPress时遇到过一些情况,我的WordPress主题遇到了错误,因为上传文件夹wp-content/uploads不存在。
显然Bluehost cPanel WordPress安装程序不会创建这个文件夹,但HostGator可以。
因此,我需要向我的主题添加代码,以检查文件夹并创建它。
当前回答
我们可以使用mkdir创建文件夹。我们也可以为它设置权限。
价值的许可 0不能读、写或执行 1只能执行 2 .只会写字 3 .可以写入和执行 4 .只能阅读 5 .可以读取和执行 6 .会读会写 7能读、写和执行
<?PHP
// Making a directory with the provision
// of all permissions to the owner and
// the owner's user group
mkdir("/documents/post/", 0770, true)
?>
其他回答
你还可以试试:
$dirpath = "path/to/dir";
$mode = "0764";
is_dir($dirpath) || mkdir($dirpath, $mode, true);
试试这个,使用mkdir:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
请注意,0777已经是目录的默认模式,并且仍然可能被当前umask修改。
像这样使用一个helper函数:
function makeDir($path)
{
$ret = mkdir($path); // use @mkdir if you want to suppress warnings/errors
return $ret === true || is_dir($path);
}
如果目录已成功创建或已经存在,则返回true;如果目录无法创建,则返回false。
一个更好的选择是这样的(不应该给出任何警告):
function makeDir($path)
{
return is_dir($path) || mkdir($path);
}
如果您想避免file_exists vs. is_dir问题,我建议您看一下这里。
我尝试了这一点,它只创建目录,如果目录不存在。它不关心是否有同名的文件。
/* Creates the directory if it does not exist */
$path_to_directory = 'path/to/directory';
if (!file_exists($path_to_directory) && !is_dir($path_to_directory)) {
mkdir($path_to_directory, 0777, true);
}
我们可以使用mkdir创建文件夹。我们也可以为它设置权限。
价值的许可 0不能读、写或执行 1只能执行 2 .只会写字 3 .可以写入和执行 4 .只能阅读 5 .可以读取和执行 6 .会读会写 7能读、写和执行
<?PHP
// Making a directory with the provision
// of all permissions to the owner and
// the owner's user group
mkdir("/documents/post/", 0770, true)
?>