looking for a script parsing a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bastler1001
Forum Newbie
Posts: 3
Joined: Sat Apr 05, 2008 5:27 am

looking for a script parsing a directory

Post by bastler1001 »

i'm looking for a php script that parses a given directory and displays the files found as links on a webpage. BUT on click it should not open the actual file but a template like

<?php
include 'header.inc';
include 'actual-file.txt';
include 'footer.inc';
?>

it should be simple like a snippet. does anybody have such a script?
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: looking for a script parsing a directory

Post by Kadanis »

First off here is a function for parsing directories and returning an array of the files inside.

Code: Select all

 
/**
 * Returns array of files in a given directory
 *
 * @param string $dir_path
 * @return array
 */
function get_dir_contents($directory){
 
        //set handle to the directory
        $handle = opendir($directory);
        
        $files = array();
        
        //loop over files
        while (false != ($file = readdir($handle))){                    
            if ($file != '.' && $file != '..'){
                $files[] = $file;               
            }           
        }   
        return $files;  
}
 
My suggestion for your script would then be to get the array of files, itterate over them and make the link list. However, each link could just be to a template and pass a variable on the URL which could be used in the template.

For example

Code: Select all

 
<?php
#link_list.php
 
#script with the function above in it
require_once("function.php"); 
 
#use function
$files = get_dir_contents('/path/to/dir');
 
#loop over files and make html links with vars passed on URL
foreach ($files as $file){
    echo '<a href="template.php?f=' . $file . '">' . $file . '</a><br />';
}
 
?>
 
<?php
#template.php
 
#check var has been passed from your page
$file = (isset($_GET['f'])) ? $_GET['f'] : false;
 
include 'header.inc';
include $file;
include 'footer.inc';
 
?>
 
bastler1001
Forum Newbie
Posts: 3
Joined: Sat Apr 05, 2008 5:27 am

Re: looking for a script parsing a directory

Post by bastler1001 »

thank you very much Mr. Kadanis. the code is simple and lovely. it works except for one thing: template.php displayes the filename instead of the file content.

now i modified that and extended the code to a news script package PlainNews. and the package works except for one thing: index.php shows links as file names instead of headlines.

can somebody please improve my PlainNews to show links as headlines and not file names?

you can download PlainNews 1.0 Alpha from here:

http://www.mediafire.com/?1vympjgxjs0
Post Reply