|
Posted by andrew on 03/30/07 15:32
Hi
I have the following script, which lists files within a specified web
directory (as long as they are valid extension types). It works, but I
would like the file names to be links (i.e. with hrefs) that allow you
to open that file directly from the page. Purpose of the script is
purely to display a list of photos within the directory, with links to
view those photos. PHP is not my strong-point. Any suggestions would
be very gratefully received... Here's the script....
//This function retrieves all files within a directory (non recursive)
and outputs them onto the page
//You can limit the type of files to retrieve, such as images only, as
dictated by the file's extension
function getfiles($dirname=".") {
$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; //valid image
extensions
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)) //if this file is a valid image
echo "$file <br />";
}
closedir($handle);
}
return($files);
}
//EXAMPLE USAGE:
getfiles(); //List all image files within the directory the PHP script
is in
getfiles("/home/myserver/public_html/george"); //List all image files
within a specific directory on the server
Navigation:
[Reply to this message]
|