建立文件夾(若是尚不存在)

我在使用Bluehost進行WordPress安裝時遇到了一些狀況,因爲沒有上傳文件夾wp-content/uploads ,個人WordPress主題遇到了錯誤。 php

顯然,儘管HostGator能夠,Bluehost cPanel WordPress安裝程序不會建立此文件夾。 session

所以,我須要向主題添加代碼,以檢查文件夾並以其餘方式建立該文件夾。 app


#1樓

這是丟失的一塊。 您須要像這樣在mkdir調用中將「遞歸」標誌做爲第三個參數(布爾值true)傳遞: wordpress

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

#2樓

if (!is_dir('path_directory')) {
    @mkdir('path_directory');
}

#3樓

對於登陸站點,我須要相同的內容。 我須要用兩個變量建立一個目錄。 $ directory是我要使用用戶許可證號建立另外一個子文件夾的主文件夾。 函數

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

#4樓

在WordPress中,還有一個很是方便的函數wp_mkdir_p ,它將以遞歸方式建立目錄結構。 spa

資料來源:- .net

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

#5樓

嘗試這個: code

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

請注意, 0777已是目錄的默認模式,而且仍可能被當前的umask修改。 orm

相關文章
相關標籤/搜索