I've been wanting to do one of these for some time as I find them very handy.
Basically, this function I've created allows you to get files from any directory on your webserver.
The code can be found below:
The code above will return something similar to this:PHP Code:<?php
function GetFiles( $directory, $output_array = false, $exts = array( "png", "jpeg", "jpg", "bmp", "gif" ) )
{
if ( is_dir( $directory ) )
{
$scan = scandir( $directory );
$return = array();
foreach( $scan as $file )
{
$ext = strtolower( end( explode( ".", $file ) ) );
if ( $file !== "." && $file !== ".." & in_array( $ext, $exts ) == true )
{
if ( $output_array == true )
{
$return[] = $file;
}
else
{
echo $file . "<br />";
}
}
}
if ( $output_array == true ) return $return;
}
else
{
echo "Directory {$directory} is invalid!";
}
}
GetFiles( "images/work" );
?>
The code above will return something similar to:PHP Code:<?php
function GetFiles( $directory, $output_array = false, $exts = array( "png", "jpeg", "jpg", "bmp", "gif" ) )
{
if ( is_dir( $directory ) )
{
$scan = scandir( $directory );
$return = array();
foreach( $scan as $file )
{
$ext = strtolower( end( explode( ".", $file ) ) );
if ( $file !== "." && $file !== ".." & in_array( $ext, $exts ) == true )
{
if ( $output_array == true )
{
$return[] = $file;
}
else
{
echo $file . "<br />";
}
}
}
if ( $output_array == true ) return $return;
}
else
{
echo "Directory {$directory} is invalid!";
}
}
echo "<pre>";
print_r ( GetFiles( "images/work", true ) );
echo "</pre>";
?>
You can make the function return an array by adding ", true" after your directory.
Hope this script helps.
m0nsta.



Reply With Quote![[PHP] Simple File Directory Scanner](http://ragezone.com/hyper728.png)

