Page 1 of 1
looking for a script parsing a directory
Posted: Mon Apr 07, 2008 4:25 am
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?
Re: looking for a script parsing a directory
Posted: Mon Apr 07, 2008 5:16 am
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';
?>
Re: looking for a script parsing a directory
Posted: Tue Apr 08, 2008 10:14 am
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