【问题标题】:RecursiveDirectoryIterator gives exceptionRecursiveDirectoryIterator 给出异常
【发布时间】:2015-04-07 11:58:05
【问题描述】:

RecursiveDirectoryIterator 给出意外值异常

$path= WEB."sync_content/offer/";
  $files = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($path),
                RecursiveIteratorIterator::LEAVES_ONLY
            );

 foreach ($files as $name => $file) {
        $fn=$file->getFilename();
        if($fn!='.' && $fn!='..' && !is_dir($fn)) {
            $filePath = $file->getRealPath();
            $zip->addFile($filePath,$fn);   
        }
    }

错误是:

致命错误 未捕获的异常“UnexpectedValueException”,带有消息“RecursiveDirectoryIterator::__construct(http://localhost/base/sync_content/offer/):无法打开目录:未在 C:\xampp\htdocs\base\classes\campaigns.class.php 249 中实现

【问题讨论】:

  • 尝试直接给出完整路径(硬编码)并检查它是否工作?
  • 我试过了......但仍然是同样的错误...... :(

标签: php scandir


【解决方案1】:

您似乎正在使用 URL ("http://localhost/...") 访问本地存储,这对 RecursiveIteratorIterator 不起作用。您最好将$path 设置为直接指向本地目录,例如$path = "/full/path/to/base/sync_content/offer/"

【讨论】:

    【解决方案2】:

    您可以使用try catch 获取特定的错误消息,在我的情况下是permission issues

    【讨论】:

      【解决方案3】:

      您不能将 RecursiveIteratorIterator 用于网页。这只能应用于本地目录。 URL 不是目录,这就是它失败的原因。

      【讨论】:

      • 在“localhost/base/sync_content/offer”这个文件夹里面有图片,比如1.jpg 2.jpg,在这种情况下怎么办
      • 很遗憾你不能。类构造函数只接受路径,不接受 url。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2013-01-23
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多