PHP - Iterating through folders and displaying HTML content

I'm currently trying to develop a method to get an overview of all my various web templates that I have created and (legally) downloaded over the years. I thought about how to display them as WordPress , browsing my templates with a small preview window, displaying a specific file with styles and everything.

How can I split them into rows and columns and create an Ajax modal window open for preview and pagination, etc.?

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

I haven’t worked much with directories in PHP, and the only links and code strings I have found so far are just a list of all the files in a particular directory, for example, what it contains.

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

+1
source share
1 answer

glob('*.html') will work if they are all in the same directory.

If you want to go through the file tree - checking everything in the current directory and in subdirectories and subdirectories of subdirectories (etc.) - then you have several options.

One could use the unix find with one of the PHP system call methods. Sort of:

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 can do is write a recursive function that uses chdir and readdir or possibly 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 name it as follows:

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

Or write dir_walk so that it accepts an object with a method call, which you can do internally. Some options are possible here.

+1
source

All Articles