PHP Directory Iterator不能用作数组

时间:2013-11-16 07:05:43

标签: php directory traversal

这里发生了一件奇怪的事情,我不确定为什么会这样。

我创建了一个类,它自动回显css和js的include标签。它使用php中的direcotryiterator。以下是两种方法:

   function _setup_js() {
            // Check for js folder in the $_active url
            if (file_exists(SITE_ROOT . "/view/" . $this->view . "/js")) {
                // Loop through them and echo js includsion
                $js_dir = new DirectoryIterator(SITE_ROOT . "/view/" . $this->view . "/js");
                foreach ($js_dir as $fileinfo) {
                    if ($fileinfo->getFilename() !== "." AND $fileinfo->getFilename() !== "..") {
                        echo "<script type='text/javascript' src='" . SITE_URL . "view/" . $this->view . "/js/" . $fileinfo->getFilename() . "'></script>";
                    }
                }
            }

        }

        function _setup_css() {
            // Check for js folder in the $_active url
            if (file_exists(SITE_ROOT . "/view/" . $this->view . "/css")) {
                // Loop through them and echo js includsion
                $css_dir = new DirectoryIterator(SITE_ROOT . "/view/" . $this->view . "/css");
// The following line is line 30                
foreach ($css_dir as $fileinfo) {
                    if ($fileinfo->getFilename() !== "." AND $fileinfo->getFilename() !== ".." AND $fileinfo['extension'] === "css") {
                     echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . SITE_URL . "view/" . $this->view . "/css/" . $fileinfo->getFilename() . "\">";
                    }
                }
            }
        }

_setup_js()方法适用于飞行颜色,但当我调用_setup_css()方法时,它会吐出此错误:

[error] 6545#0: *105 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Cannot use object of type DirectoryIterator as array in /Users/usr/Projects/com.project.sandbox/lib/lib_frontendauto.php on line 30

正如您所看到的,除了文件类型调整之外,这些方法几乎完全相同。

1 个答案:

答案 0 :(得分:2)

尝试更改此内容:

AND $fileinfo['extension'] === "css") {

对此:

AND $fileinfo->getExtension () === "css") {