Php 如果没有';不存在

Php 如果没有';不存在,php,wordpress,directory,Php,Wordpress,Directory,我在WordPress安装中遇到过一些情况,我的WordPress主题遇到了错误,因为uploads文件夹wp content/uploads不存在 显然,Bluehost WordPress安装程序并没有创建此文件夹,但确实创建了 因此,我需要在我的主题中添加代码,以检查文件夹并以其他方式创建它。尝试使用以下方法: 请注意,0777已经是目录的默认模式,并且仍然可以由当前的umask修改。像这样的助手函数如何: function makeDir($path) { $ret = mkd

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

显然,Bluehost WordPress安装程序并没有创建此文件夹,但确实创建了

因此,我需要在我的主题中添加代码,以检查文件夹并以其他方式创建它。

尝试使用以下方法:


请注意,
0777
已经是目录的默认模式,并且仍然可以由当前的umask修改。

像这样的助手函数如何:

function makeDir($path)
{
     $ret = mkdir($path); // use @mkdir if you want to suppress warnings/errors
     return $ret === true || is_dir($path);
}
mkdir('path/to/directory', 0755, true);
如果目录已成功创建或已存在,则返回
true
;如果目录无法创建,则返回
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


可以通过提供停止级别来改进,这样,如果超出用户文件夹或其他内容,它就会失败,还可以通过包括权限来改进。

这里是缺少的部分。在调用中,您需要将“recursive”标志作为第三个参数(布尔值true)传递,如下所示:

function makeDir($path)
{
     $ret = mkdir($path); // use @mkdir if you want to suppress warnings/errors
     return $ret === true || is_dir($path);
}
mkdir('path/to/directory', 0755, true);

我需要一个登录网站同样的东西。我需要创建一个包含两个变量的目录。 $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);
}

WordPress中还有一个非常方便的函数,可以递归地创建目录结构

参考资料来源:-

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;
}
函数wp_mkdir_p($target){
$wrapper=null;
//取消协议
如果(wp_是_流($target)){
列表($wrapper,$target)=分解('://',$target,2);
}
//来自php.net/mkdir用户提供的注释
$target=str_replace(“//”、“/”、$target);
//将包装器放回目标上
如果($wrapper!==null){
$target=$wrapper.'://'.$target;
}
//在某些PHP版本下,安全模式会失败,并以斜杠结尾。
$target=rtrim($target,'/');//使用rtrim()而不是untrailingslashit,以避免格式化.php依赖项。
if(空($target))
$target='/';
如果(文件_存在($target))
返回@is_dir($target);
//我们需要找到存在的父文件夹的权限并继承该权限。
$target\u parent=dirname($target);
而('.!=$target\u parent&!is\u dir($target\u parent)){
$target\u parent=dirname($target\u parent);
}
//获取权限位。
如果($stat=@stat($target\u parent)){
$dir_perms=$stat['mode']&0007777;
}否则{
$dir_perms=0777;
}
if(@mkdir($target,$dir_perms,true)){
//如果设置了修改$dir_perms的umask,则必须使用chmod()正确地重新设置$dir_perms
如果($dir\u perms!=($dir\u perms&~umask()){
$folder_parts=explode('/',substr($target,strlen($target_parent)+1));

对于($i=1;$i递归创建目录路径:

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

受Python

启发,这是没有错误抑制的最新解决方案:

if (!is_dir('path/to/directory')) {
    mkdir('path/to/directory');
}
您也可以尝试:

$dirpath = "path/to/dir";
$mode = "0764";
is_dir($dirpath) || mkdir($dirpath, $mode, true);

创建文件夹的更快方法:

if (!is_dir('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);
}
如果文件夹不存在,则创建该文件夹 考虑到问题的环境

  • WordPress
  • 网络主机服务器
  • 假设它的Linux不是运行PHP的Windows
并引述:

boolmkdir(字符串$pathname[,int$mode=0777[,bool$recursive= FALSE[,资源$context]])

手册中说,唯一需要的参数是
$pathname

因此,我们可以简单地编写代码:

<?php
error_reporting(0); 
if(!mkdir('wp-content/uploads')){
   // todo
}
?>

说明: 我们不必传递任何参数或检查文件夹是否存在,甚至不必传递模式参数,除非需要;原因如下:

  • 该命令将使用0755权限(共享托管文件夹的默认权限)或0777命令的默认权限创建文件夹
  • 在运行PHP的Windows主机上,
    模式被忽略
  • 如果文件夹存在,
    mkdir
    命令已经内置了checker;因此我们需要检查return only True | False;它不是一个错误,只是一个警告,并且默认情况下在托管服务器中禁用了警告
  • 根据速度,如果禁用警告,则速度更快
这只是研究问题的另一种方式,而不是要求更好或最理想的解决方案


在PHP7、生产服务器、Linux上测试,我们应该始终模块化我们的代码,我已经在下面编写了相同的检查。。。 我们首先检查目录,如果目录不存在,我们就创建目录

$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;
    }
}
$boolderpresents=$this->CheckDir($DirectoryName);
如果(!$boolderpresents){
$boolCreateDirectory=$this->CreateDirectory($DirectoryName);
if($boolCreateDirectory){
echo“创建成功”;
}
}
函数CheckDir($DirName){
如果(文件_存在($DirName)){
echo“Dir存在
”; 返回true; }否则{ 回声“Dir不存在
”; 返回false; } } 函数CreateDirectory($DirName){ if(mkdir($DirName,0777)){ 返回true; }否则{ 返回false; } }
如果您想避免
文件存在
VS
is\u 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;
    }
}
/* 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( string $pathname [, int $mode = 0777 [, bool $recursive = FALSE [, resource $context ]]] ) : bool
$structure = './depth1/depth2/depth3/';
if (!file_exists($structure)) {
    mkdir($structure);
}
if (!is_dir('path/to/directory')) {
    if (!mkdir('path/to/directory', 0777, true) && !is_dir('path/to/directory')) {
        throw new \RuntimeException(sprintf('Directory "%s" was not created', 'path/to/directory'));
    }
}
$path = 'path/to/directory';
if ( wp_mkdir_p( $path ) ) {
    // Directory exists or was created.
}
if (!is_dir('path/to/directory')) mkdir('path/to/directory', 0777, true);