gpt4 book ai didi

php - fatal error : Uncaught exception 'RuntimeException'

转载 作者:行者123 更新时间:2023-12-03 05:04:13 27 4
gpt4 key购买 nike

我运行的是 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;
}

最佳答案

将其包含在 try-catch block 中?

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;

关于php - fatal error : Uncaught exception 'RuntimeException' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1954202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com