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

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

因此,我需要向我的主题添加代码,以检查文件夹并创建它。


当前回答

作为现有解决方案的补充,一个效用函数。

function createDir($path, $mode = 0777, $recursive = true) {
  if(file_exists($path)) return true;
  return mkdir($path, $mode, $recursive);
}

createDir('path/to/directory');

如果已经存在或成功创建,则返回true。否则返回false。

其他回答

我们应该始终模块化我们的代码,我在下面写了同样的检查…

我们先查一下目录。如果该目录不存在,则创建该目录。

$boolDirPresents = $this->CheckDir($DirectoryName);

if (!$boolDirPresents) {
        $boolCreateDirectory = $this->CreateDirectory($DirectoryName);
        if ($boolCreateDirectory) {
        echo "Created successfully";
      }
  }

function CheckDir($DirName) {
    if (file_exists($DirName)) {
        echo "Dir Exists<br>";
        return true;
    } else {
        echo "Dir Not Absent<br>";
        return false;
    }
}
     
function CreateDirectory($DirName) {
    if (mkdir($DirName, 0777)) {
        return true;
    } else {
        return false;
    }
}

这是缺失的部分。你需要在mkdir调用中传递'recursive'标志作为第三个参数(布尔值true),如下所示:

mkdir('path/to/directory', 0755, 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)
  
?>

作为现有解决方案的补充,一个效用函数。

function createDir($path, $mode = 0777, $recursive = true) {
  if(file_exists($path)) return true;
  return mkdir($path, $mode, $recursive);
}

createDir('path/to/directory');

如果已经存在或成功创建,则返回true。否则返回false。

我需要一个登录网站同样的东西。我需要创建一个包含两个变量的目录。

$目录是主文件夹,我想在其中创建另一个带有用户许可证号码的子文件夹。

include_once("../include/session.php");

$lnum = $session->lnum; // Users license number from sessions
$directory = uploaded_labels; // Name of directory that folder is being created in

if (!file_exists($directory . "/" . $lnum)) {
    mkdir($directory . "/" . $lnum, 0777, true);
}