PDA

View Full Version : Retrieve and output certain files within a directory


WA
11-05-2005, 10:23 PM
I thought I'd kick start this forum by posting a PHP code I put together yesterday to allow me to retrieve all files within a directory, limited by file types (ie: images only). I needed it to dynamically populate a JavaScript slideshow with all images from a directory without maually specifying each image. Here it is:


//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


Example output:

backgr10.jpg
backgr11.jpg
backgr12.jpg
backgr13.jpg
backgr14.jpg
backgr15.jpg
backgr16.jpg


Reference(s) used: http://ca.php.net/manual/en/function.readdir.php

gsnedders
11-06-2005, 12:58 PM
Under that, a file such as blah.jpeg.html would show up.

Try replacing if(stristr($file, ".".$ext[$i]))
With:if (strtolower(substr(strrchr($file, '.'), 1)) == strtolower($ext[$i]))

marek_mar
11-06-2005, 01:11 PM
How about changing

for($i=0; $i<sizeof($ext); $i++)
if(stristr($file, ".".$ext[$i]))
echo "$file <br />";

to

if(in_array(pathinfo($file, PATHINFO_EXTENSION), $ext)
{
print $file;
}

WA
11-06-2005, 07:45 PM
Hi Error 404:
You read my mind. that Using regular expressions to properly detect a valid image format was one of the things I knew I had to add. Thanks for the modification.

WA
11-08-2005, 09:48 PM
I've just edited the code above to use regular expressions to better filter out a valid image based on its extension.

shinko_metsuo
11-21-2005, 09:42 PM
How would I get it to list directories too?

gsnedders
11-21-2005, 11:12 PM
//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) || is_dir($file)) //if this file is a valid image or folder
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


Untested, but should work.

shinko_metsuo
11-21-2005, 11:25 PM
Thanks a lot :thumbsup:

Element
12-14-2005, 07:25 PM
Also

"(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"
Can easily be
"\.(jpg|jpeg|png|gif|bmp)$"
With preg_match()

That might not be the correct regular expression, I am horrible with it, but I used that for my version that I used.