【问题标题】:php directoryIterator - path based on season?php directoryIterator - 基于季节的路径?
【发布时间】:2011-08-15 08:42:50
【问题描述】:

大家好, 这工作正常:

$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);

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

【问题讨论】:

  • get_template_directory() 是一个 wordpress 特定的函数,对代码无关紧要 - 只是返回目录的绝对路径。

标签: php iterator directory


【解决方案1】:

我认为有 3 种可能性..

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

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

  3. 你使用的是PHP4所在的类 DirectoryIterator 不存在

【讨论】:

    【解决方案2】:

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

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

    Throws an UnexpectedValueException if the path cannot be opened.
    

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

    【讨论】:

      【解决方案3】:

      我认为这应该可以正常工作,我确实想知道为什么会出现白屏。这通常发生在您收到致命错误并且您的 error_reporting 设置不正确时。

      请将您的 error_reporting 设置为 E_ALL:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-11
        • 2020-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-10
        • 1970-01-01
        相关资源
        最近更新 更多