致命错误:未捕获的异常 '的RuntimeException'
我运行PHP版本5.2.11致命错误:未捕获的异常 '的RuntimeException'
当我运行下面的功能:
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的虚拟目录下。我的脚本期望主题安装在主根网址之外。
例如,在这种情况下,主题是安装在:http://www.example.com/testing123/wp-content/themes/mytheme
不过,我期待这样的:http://www.example.com/wp-content/themes/mytheme
和SO ...因为它
我的路径功能失效不考虑它可以安装在虚拟目录下。
我怎么能在我的这个功能的喂养占这个场景?
$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;
}
把它放在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;
感谢Maurice,我相信这也许是最好的选择。我已经更新了我的问题,因为我发现了有关导致错误的更多信息。它实际上是由于虚拟目录下的意外位置。 我的代码... $ mydir = get_dirs($ _ SERVER ['DOCUMENT_ROOT']。'/ wp-content/themes/mytheme/images'); 对“testing123”子目录没有帐号。 – 2009-12-23 17:47:11
类是存在的,但显然你的图片目录不是:
/home/test/test.com/wp-content/themes/mytheme/images
消息之中的关键部分:
failed to open dir: No such file or directory
或者,如果它的存在,PHP没有权限读取它。
它看起来像第三行是告诉你该目录无法找到:
failed to open dir: No such file or directory
我会从脚本检查你的路径被称为并调整适当
如果它说,它未能打开你的图片目录,你为什么认为错误在编码中? – 2009-12-23 17:34:37
呃......它看起来像无法找到目录,它没有实例化DirectoryIterator的问题。 – mozillalives 2009-12-23 17:35:37