我在Bluehost上安装WordPress时遇到过一些情况,我的WordPress主题遇到了错误,因为上传文件夹wp-content/uploads不存在。
显然Bluehost cPanel WordPress安装程序不会创建这个文件夹,但HostGator可以。
因此,我需要向我的主题添加代码,以检查文件夹并创建它。
我在Bluehost上安装WordPress时遇到过一些情况,我的WordPress主题遇到了错误,因为上传文件夹wp-content/uploads不存在。
显然Bluehost cPanel WordPress安装程序不会创建这个文件夹,但HostGator可以。
因此,我需要向我的主题添加代码,以检查文件夹并创建它。
当前回答
在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;
}
其他回答
这里有一些更通用的东西,因为它出现在谷歌上。虽然细节更具体,但这个问题的标题更具有普遍性。
/**
* recursively create a long directory path
*/
function createPath($path) {
if (is_dir($path))
return true;
$prev_path = substr($path, 0, strrpos($path, '/', -2) + 1 );
$return = createPath($prev_path);
return ($return && is_writable($prev_path)) ? mkdir($path) : false;
}
这将采用一条路径,可能包含一长串未创建的目录,并继续向上移动一个目录,直到到达一个现有目录。然后,它将尝试在该目录中创建下一个目录,直到创建所有目录为止。如果成功则返回true。
它可以通过提供一个停止级别来改进,这样它就会在超出用户文件夹或其他地方时失败,并包括权限。
像这样使用一个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);
}
我们应该始终模块化我们的代码,我在下面写了同样的检查…
我们先查一下目录。如果该目录不存在,则创建该目录。
$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);