【问题标题】:Fatal error: Uncaught exception 'RuntimeException'致命错误:未捕获的异常“RuntimeException”
【发布时间】:2009-12-23 17:32:13
【问题描述】:

我正在运行 PHP 版本 5.2.11

当我运行下面的函数时:

function get_dirs($path = '.') {
    $dirs = array();
    foreach (new DirectoryIterator($path) as $file) {  // This is line 462.
        if ($file->isDir() && !$file->isDot()) {
            $dirs[] = $file->getFilename();
        }
    }
    return $dirs;
}

我得到这个错误:

Fatal error: Uncaught exception 'RuntimeException' with message 'DirectoryIterator::__construct(/home/test/test.com/wp-content/themes/mytheme/images) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: failed to open dir: No such file or directory' in /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php:462 Stack trace: #0 /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php(462): DirectoryIterator->__construct('/home/test/wie...') #1 /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php(31): get_dirs('/home/test/wie...') #2 /home/test/test.com/testing123/wp-settings.php(717): include('/home/test/wie...') #3 /home/test/test.com/testing123/wp-config.php(76): require_once('/home/test/wie...') #4 /home/test/test.com/testing123/wp-load.php(30): require_once('/home/test/wie...') #5 /home/test/test.com/testing123 in /home/test/test.com/testing123/wp-content/themes/mytheme/functions.php on line 462

更新:我发现这里的问题是主题安装在主 URL 之外的虚拟目录下。我的脚本期望主题安装在主根 url 之外。

例如本案例中的主题安装在:http://www.example.com/testing123/wp-content/themes/mytheme

但是,我期待这个:http://www.example.com/wp-content/themes/mytheme

还有……

我的路径功能失败,因为它没有考虑到它可以安装在虚拟目录下。

我如何在提供此功能时考虑这种情况?

$mydir = get_dirs("$_SERVER[DOCUMENT_ROOT]/wp-content/themes/mytheme/images");

function get_dirs ($path = '.') {
    $dirs = array();
    foreach (new DirectoryIterator($path) as $file) {
        if ($file->isDir() && !$file->isDot()) {
            $dirs[] = $file->getFilename();
        }
    }

    return $dirs;
}

【问题讨论】:

  • 如果显示无法打开您的图像目录,您认为错误是在编码中吗?
  • 呃……好像找不到目录,实例化DirectoryIterator没问题。

标签: php


【解决方案1】:

将它包含在 try-catch 块中?

function get_dirs($path = '.') {
  $dirs = array();
  try{
    foreach (new DirectoryIterator($path) as $file) {  //this is line 462
    if ($file->isDir() && !$file->isDot()) {
      $dirs[] = $file->getFilename();
     }
   }
 } catch(Exception $e) {
   //log exception or process silently
   //just for test
   echo $e;
 }

 return $dirs;

【讨论】:

  • 谢谢莫里斯,我相信这也许是最好的选择。我已经更新了我的问题,因为我找到了有关导致错误的原因的更多信息。它实际上是由于虚拟目录下的意外位置。我的代码... $mydir = get_dirs($_SERVER['DOCUMENT_ROOT'].'/wp-content/themes/mytheme/images');不考虑“testing123”子目录。
【解决方案2】:

类在那里,但显然你的图像目录不是:

/home/test/test.com/wp-content/themes/mytheme/images

消息的关键部分是:

failed to open dir: No such file or directory

或者,如果它在那里,PHP 没有读取它的权限。

【讨论】:

    【解决方案3】:

    看起来第三行告诉你找不到目录:

    打开目录失败:没有这样的文件或目录

    我会从被调用的脚本中检查你的路径并进行适当的调整

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 2013-07-07
      • 2015-06-10
      • 2015-06-20
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多