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

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

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


当前回答

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

mkdir('path/to/directory', 0755, true);

其他回答

递归创建目录路径:

function makedirs($dirpath, $mode=0777) {
    return is_dir($dirpath) || mkdir($dirpath, $mode, true);
}

灵感来自Python的os.makedirs()

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

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

$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;
    }
}

对于你关于WordPress的具体问题,使用下面的代码:

if (!is_dir(ABSPATH . 'wp-content/uploads')) wp_mkdir_p(ABSPATH . 'wp-content/uploads');

函数参考:WordPress wp_mkdir_p。ABSPATH是返回WordPress工作目录路径的常量。

还有另一个名为wp_upload_dir()的WordPress函数。它返回上传目录路径,如果不存在,则创建一个文件夹。

$upload_path = wp_upload_dir();

下面的代码一般适用于PHP。

if (!is_dir('path/to/directory')) mkdir('path/to/directory', 0777, true);

函数参考:PHP is_dir()

在WordPress中,还有一个非常方便的函数wp_mkdir_p,它将递归地创建一个目录结构。

参考资料来源:

function wp_mkdir_p( $target ) {
    $wrapper = null;

    // Strip the protocol
    if( wp_is_stream( $target ) ) {
        list( $wrapper, $target ) = explode( '://', $target, 2 );
    }

    // From php.net/mkdir user contributed notes
    $target = str_replace( '//', '/', $target );

    // Put the wrapper back on the target
    if( $wrapper !== null ) {
        $target = $wrapper . '://' . $target;
    }

    // Safe mode fails with a trailing slash under certain PHP versions.
    $target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.
    if ( empty($target) )
        $target = '/';

    if ( file_exists( $target ) )
        return @is_dir( $target );

    // We need to find the permissions of the parent folder that exists and inherit that.
    $target_parent = dirname( $target );
    while ( '.' != $target_parent && ! is_dir( $target_parent ) ) {
        $target_parent = dirname( $target_parent );
    }

    // Get the permission bits.
    if ( $stat = @stat( $target_parent ) ) {
        $dir_perms = $stat['mode'] & 0007777;
    } else {
        $dir_perms = 0777;
    }

    if ( @mkdir( $target, $dir_perms, true ) ) {

        // If a umask is set that modifies $dir_perms, we'll have to re-set the $dir_perms correctly with chmod()
        if ( $dir_perms != ( $dir_perms & ~umask() ) ) {
            $folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );
            for ( $i = 1; $i <= count( $folder_parts ); $i++ ) {
                @chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );
            }
        }

        return true;
    }

    return 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);
}