目录迭代器-基于季节的路径


php directoryIterator - path based on season?

大家好,

$dir = new DirectoryIterator(get_template_directory().'/images/headers/');

然而,我不只是/headers,但我有一个/headers/summer和headers/winter目录,我想用它们作为当前季节的路径。

$summer = array(3,4,5,6,7,8,9);
$path = null;
if ( in_array(date('n'), $summer) ) {
    $path = get_template_directory().'/images/headers/summer/';
} else {
    $path = get_template_directory().'/images/headers/winter/';
}
$dir = new DirectoryIterator($path);

这是怎么回事?如果我使用这个,我的页面是空白的!

我认为有三种可能…

  1. $path不存在,检查with if(!is_dir($path)) die('no dir there');

  2. 你的函数get_template_directory

  3. 你正在使用PHP4里面的类目录迭代器不存在

我认为这应该工作得很好,我想知道为什么你得到一个白屏。这通常发生在收到致命错误并且error_reporting设置不正确时。

请将error_reporting设置为E_ALL:

error_reporting(E_ALL);
ini_set('display_errors', true);

看起来您没有启用错误报告。在脚本中执行error_reporting(E_ALL)或在apache日志中查找错误。

您可能会在页面后面得到一个错误,因为您对内容的假设不同(可能是空目录?)或对目录的错误权限(来自http://www.php.net/manual/en/directoryiterator.construct.php):

)
Throws an UnexpectedValueException if the path cannot be opened.

但没有详细的错误,这只是猜测。