dongzou9047 2011-09-16 13:35
浏览 56
已采纳

too long

Can anyone tell me the best practice for including a set of files in a directory?

For instance, if I had a directory that was /javascript, and in there it had 10 javascript files, how would I go about loading the entire directory?

This question is moot now, but for future reference;

I use the following to read files in the same manner:

<?php
    session_start();

    // Auto import all js files in the directory.

            $files = glob("*.js");
            sort($files);                   

            $jsFiles = array();
            for($i = 0; $i < count($files); $i++){
                error_log('Auto-Opening: '.$files[$i]);
                $tempString = file_get_contents($files[$i]);
                //error_log('Auto-Opened Content: '.$tempString);
                array_push($jsFiles, $tempString);
            }
?>

Sorry, I guess this needs clarification:

The code I have included is used in an entirely seperate part of my project, it includes the RAW string format of a directory of javascript files for my processing purposes. You do not have to worry about why I am doing this.

I could use this to add script tags, and echo these files, but I am looking for a faster more elegant way to include a directory. Unlike the code above (whose javascript never gets executed by my code. I simply create the string from the files and send it to JS for parsing).

Please also note, as suggested:

$files = glob("{directory/*.js}",GLOB_BRACE);
                for($i = 0; $i < count($files); $i++){
                    include($files[$i]);
                }

Does nothing but echo those files to the screen, without script tags.

  • 写回答

4条回答 默认 最新

  • doushouj966020 2011-09-16 14:00
    关注

    I could use this to add script tags, and echo these files, but I am looking for a faster more elegant way to include a directory. Unlike the code above (whose javascript never gets executed by my code. I simply create the string from the files and send it to JS for parsing).

    If you want the output of the PHP script to get executed as javascript, you need to add a proper header:

    header("Content-type: text/javascript");
    foreach (glob('/path/to/files/*.js') as $file)
    {
        readfile($file);
    }
    

    I am reading the directory as strings because I am doing some parsing with the raw JS (but that is separate from what I am doing here). I know to use php to include a bunch of files by statically coding them in, I want to do it on a by directory basis that is dynamic.

    Unless you have some sort of file naming convention going on, remember that the order of the files is going to be basically limited to what you can do with php's array sorting functions on the file names. JS and CSS are often very dependent on code or files being executed or read in a certain order, so depending on what it is you're actually trying to accomplish (still very unclear), this might be an awful idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 chemex:docker安装chemex过程中遇到问题
  • ¥15 关于遇到一个python,django,redis,uwsgi,nginx搭的一个企业微信修改AD密码的平台的问题!
  • ¥15 读取parquet文件某一列的数据但是输出是整个列名和格式
  • ¥15 机动目标 制导律建模问题
  • ¥100 求Java socks 转发实现Demo
  • ¥20 随身WiFi移动网络访问不了
  • ¥50 RAD_XE11.3获取android11手机的IMEI码
  • ¥15 linux的gcc命令报错
  • ¥20 如何再GIS用海岸线建立缓冲区
  • ¥15 codeblock遇到问题了,求帮助😭