我正在运行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
AND 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;
}
答案 0 :(得分:4)
将它封闭在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;
答案 1 :(得分:2)
班级在那里,但显然你的图片目录不是:
/home/test/test.com/wp-content/themes/mytheme/images
信息的关键部分是:
failed to open dir: No such file or directory
或者,如果它存在,PHP没有权限从中读取。
答案 2 :(得分:0)
看起来第三行告诉你找不到目录:
无法打开目录:没有这样的文件或目录
我会从被调用的脚本中检查你的路径并适当调整