dtl4521 2010-05-04 21:42
浏览 80
已采纳

PHP - 遍历文件夹并显示HTML内容

I'm currently trying to develop a method to get a overview of all my different web templates I've created and (legally) downloaded over the years. I thought about displaying them like WordPress is previewing its templates with a small preview window, displaying the concrete file with styles and everything.

How do I divide them into rows and columns and create Ajax modal window open on preview and pagination and so on?

I believe I can manage, but it is the concept itself about iterating over several folders and then finding all index.htm and index.html pages and displaying them.

I've not worked very much with directories in PHP and the only references and code stumps I've found so far is just to list all the files in a certain directory like, what it contains.

Is there a script, a function, snippet or just some information to create such a (probably simple) preview function?

  • 写回答

1条回答 默认 最新

  • duanjuete9206 2010-05-05 00:34
    关注

    glob('*.html') will work if they're all in one directory.

    If you want to walk a file tree -- checking everything in the current directory and in subdirectories and subdirectories of subdirectories (etc) -- then you have a couple of options.

    One would be to use the unix find command with one of the methods of PHP system invocation. Something like:

    find <search_root_dir> -name "*.html" -print

    will get you output that looks something like

    search_root_dir/blah.html
    search_root_dir/foo.html
    search_root_dir/subdir/baz.html
    search_root_dir/subdir/bah.html
    ...
    

    Another thing you could do is write a recursive function that uses chdir and readdir or maybe scandir, something like:

    function dir_walk($start_dir,$func) {
        $entries = scandir($start_dir);
        foreach($entries as $entry) {
            if($entry == '.' || $entry == '..') {
                /*skip these*/
            } else if(is_dir($entry)) {
                dir_walk($start_dir.'/'.$entry,$func);
            } else $func($start_dir.'/'.$entry);
        }
    }
    

    Then, write another function:

    $html_files = array();
    function record_html_files($filename) {
       global $html_files;
       if(strpos($filename,'*.html') === (strlen($filename) - 6))
         $html_files[] = $filename;
    }
    

    And call it like this:

    dir_walk('/path/to/search/root','record_html_files');
    

    Or, write dir_walk so it accepts an object with a method call you can make inside. There's some variations possible here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)