php directoryIterator - 基于季节的路径?

时间:2011-04-29 08:58:28

标签: php iterator directory

嘿伙计们, 这很好用:

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

但是我没有/ header但我有一个/ 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);

这有什么问题?如果我使用它,我的页面是空白的!

3 个答案:

答案 0 :(得分:2)

我认为有3种可能性......

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

  2. 你的函数get_template_directory 抛出致命的

  3. 你正在使用PHP4所在的课程 DirectoryIterator不存在

答案 1 :(得分:1)

我认为这应该可以正常工作,我不知道为什么你会得到一个白色的屏幕。当您收到致命错误并且未正确设置error_reporting时,通常会发生这种情况。

请将您的error_reporting设置为E_ALL:

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

这应该显示发生的任何错误。

答案 2 :(得分:0)

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

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

Throws an UnexpectedValueException if the path cannot be opened.

但是没有关于错误的细节,这只是猜测。