PHP directory contents displayer



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

.



Relevant Pages

  • Re: local and my in subroutines
    ... I have a subroutine in the script below, printsub that errors when it is ... The second sub in this script works using: ... # Create extenions for log files as the older ones have a bz2 extension. ... foreach my $end ...
    (perl.beginners)
  • Re: Uploading Images via PHP
    ... >>uploading a malicious script? ... The web server might be happy to execute them for you if the uploads are ... generally be based on file extensions, so an extension whitelist may be ...
    (alt.php)
  • Re: Finding a File by the Extension.
    ... The problem is I need the script to go out search the C: ... drive and find the file by the file extension not the file name. ... 'Rumba Startup ... OK, then, but *which* .WPM file do you want to deal with, the first one your ...
    (microsoft.public.scripting.vbscript)
  • Re: Having problems creating a installer for my app due to permissions?
    ... script that they could also download to automatically add the file ... or create file extension mappings. ... install the requires admin priveleges or do it the way you have been. ... are nix developers and administrators so we tend not to download too ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finding a File by the Extension.
    ... The problem is I need the script to go out search the C: ... drive and find the file by the file extension not the file name. ... 'Rumba Startup ... OK, then, but *which* .WPM file do you want to deal with, the first one your ...
    (microsoft.public.scripting.vbscript)