【问题标题】:mkdir() says theres no such directory and fails?mkdir() 说没有这样的目录并且失败了?
【发布时间】:2013-02-07 08:44:59
【问题描述】:

我可能做一些非常简单的错误,但是当我尝试创建一个目录时(使用刚刚执行的插入变量作为最后一个文件夹名称),我得到了错误:

警告:mkdir() [function.mkdir]:/home/blah/blah 中没有这样的文件或目录

用代码:

if (!is_dir("images/listing-images/rent/'.$insertID.")) {
        //make new directory with unique id
   mkdir("images/listing-images/rent/'.$insertID."); 
}

当然该目录不存在.. 我现在正在尝试制作它?糊涂了!

【问题讨论】:

  • 请解释一下"images/listing-images/rent/'.$insertID."字符串是什么意思
  • 现在要创建的目录的父目录在吗?
  • 不要混淆。我认为您要在其中创建目录的目录不存在。
  • .. 所以我忘了先创建 /rent 目录。觉得我工作太久了!谢谢大家

标签: php upload mkdir


【解决方案1】:

这是因为您的文件系统中没有 images/listing-images/rent 路径。

如果您想创建整个路径 - 只需将第三个参数作为 true 传递:

mkdir('images/listing-images/rent/'.$insertID, 0777, true);

也有可能您当前位于错误的目录中。如果是这种情况 - 您需要使用 chdir() 更改当前目录或指定完整路径。

【讨论】:

  • 与上述相同.. 仅快 1 秒
  • 这应该是正确的答案,因为“正确”答案已被标记为正确的代码中没有语法错误。
【解决方案2】:

假设您使用的是 PHP > 5.0.0,请尝试使用mkdir("path", 0777, true); 启用递归创建目录(参见此处:http://php.net/manual/en/function.mkdir.php)。

【讨论】:

  • 唯一一个与 PHP 5.0 规则相关的解释
【解决方案3】:

您的字符串中有错误:

mkdir("images/listing-images/rent/'.$insertID.");

应该是:

mkdir("images/listing-images/rent/$insertID");

【讨论】:

  • 我确定他的错误不是由此引起的,而是由于缺少images/listing-images/rent
  • 那么你的最终答案是什么?代码中没有语法错误。该字符串在语法上是正确的(尽管毫无意义)。正如你所说 - 这应该是一个评论:-)
  • 如果您愿意,我可以更改 syntax 这个词,如果您认为这样会使答案更准确
  • 您在“应该”中提出的更改不能更改行为 - 代码会抛出相同的错误
  • 我不接受这个作为答案,以帮助我消除无意义的代码,尽管我的问题的 cmets 解决了我的实际问题:)
【解决方案4】:
  • recursive 允许创建在 路径名。
  • 但对我不起作用!!因为这就是我想出的!
  • 而且效果非常好!!

$upPath = "../uploads/RS/2014/BOI/002"; // 完整路径
$tags = explode('/' ,$upPath); // 分解完整路径
$mkDir = "";

foreach($tags as $folder) {          
    $mkDir = $mkDir . $folder ."/";   // make one directory join one other for the nest directory to make
    echo '"'.$mkDir.'"<br/>';         // this will show the directory created each time
    if(!is_dir($mkDir)) {             // check if directory exist or not
      mkdir($mkDir, 0777);            // if not exist then make the directory
    }
}

【讨论】:

    【解决方案5】:

    在我的例子中,$insertID 是通过连接从一些数据作为字符串生成的

    $insertID=$year.$otherId;
    

    我像这样简单地重写了代码,错误消失了:

    $insertID=(int)($year.$otherId);
    

    【讨论】:

      【解决方案6】:

      可能真正的错误是他忘记了一个额外的顶点。

      这个:

      mkdir("images/listing-images/rent/'.$insertID.");
      

      内部:

      /'.$insertID."
      

      正确版本:

      /".$insertID
      

      扩展正确版本:

      mkdir("images/listing-images/rent/".$insertID);
      

      【讨论】:

        【解决方案7】:
        $path = 'd:\path\to\my\file';
        mkdir($path, null, true);
        

        这是从 php 手册中复制的。最后一个参数“true”允许创建子文件夹

        【讨论】:

          【解决方案8】:

          你不应该使用 is_dir() 来检查是否存在,你也需要 file_exists() 。试试:

          if (file_exists("images/listing-images/rent/$insertID") {
              mkdir("images/listing-images/rent/$insertID.");
          }
          

          已采取'。出,因为它看起来像一个语法错误,但你可能有正当理由保留它。

          如果 mkdir 仍然失败,可能是 images/listing-images/rent 不存在,如果存在,您必须单独创建。

          【讨论】:

          • "你不应该使用 is_dir()" --- 对此有何解释?
          • 实际上忽略第一部分,只是检查了 is_dir 文档,它也检查了是否存在,所以它只是父目录不存在。
          • 应该注意is_dir 缓存结果。请参阅 php.net 页面中的注释。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-02
          • 1970-01-01
          • 1970-01-01
          • 2022-11-10
          • 2013-01-13
          • 2021-05-13
          相关资源
          最近更新 更多