Hi,

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 a displaying them like Wordpress is previewing it’s templates view a small preview windows, displaying the concrete file with styles and everything.

How to 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 iterate over several folders then find all index.htm / 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.

I would be really grateful if someone knew about a script, a function, snippet or just could get me a nudge in the right direction to create such a (probably simple) preview function.

Sincere
Mestika

Recommended Answers

All 2 Replies

I have no doubt that there are many ways to do it. Maybe this will work for you:

<?php
// Where do I run (Window or Linux) and handle accordingly
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { // Windows
        $path = "C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs";
        $command = "dir /s/b/o:g ".$path." > dirlist.txt";
}
else { // Linux
        $path = "/var/www/htdocs/";
        $command = "ls -1 -R ".$path." > dirlist.txt";
}
system($command); // execute the command.

The above code will use the native commands and recursion to put everything in dirlist.txt. Now all you have to do is open dirlist.txt, read it and do whatever you want with the content.

<?php
$my_file = "dirlist.txt";
$fh = fopen($my_file, 'r');
// use a loop with fgets($fh) to read the file and process the contents
fclose($fh);
?>

Depending on the complexity of the directory structure you're trying to navigate i'd recommend looking at the Standard PHP Library (SPL) specifically at DirectoryIterator and RecursiveDirectoryIterator

They're going to greatly simplify navigating very complex hierarchies. The other thing that is nice is you can always wrap them with a filter iterator to filter out only particular file types or by filename etc, and then you could also wrap that in a sorting iterator, which would be more of a custom iterator, that would allow you to reorder based on whatever criteria you wanted.

Can always mix and match based on your requirements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.