如何递归链接javascript文件到HTML

时间:2017-05-22 20:40:04

标签: javascript php html

我有以下文件结构:

root - dir1 - js_dir     - file.js  
     - dir2 - inner_dir  - header.php  

目录 js_dir 包含我的javascript文件。我尝试以header.php递归方式将它们添加到我的标题中,如下所示:

header.php

<head>
    <?php

        $dir = '../../dir1';

        $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST );

        foreach ( $iterator as $path ) {
          if ($path->isDir()) {
            print('<div> DIR : ' . $path . PHP_EOL . "</div>");
          } else {
            $path_parts = pathinfo($path);
            $file_parts = $path_parts['extension'];
            if ($file_parts == "js")
            {
              $array = explode("/opt/lampp/htdocs", $path);
              unset($array[0]);
              echo $path = implode("/", $array);
              print("<div>http://localhost$path" . PHP_EOL . "</div>");
              echo("<script src='http://localhost/$path'></script>");
            }
          }
        }

        ?>
</head>

然而,我不断收到错误:

Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(../js): failed to open dir: No such file or directory in /opt/lampp/htdocs/root

我怎样才能让它发挥作用?

提前谢谢大家。

0 个答案:

没有答案