Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2005, 09:23 PM   PM User | #1
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
Retrieve and output certain files within a directory

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:

PHP Code:
//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
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.

Last edited by WA; 11-08-2005 at 08:45 PM..
WA is offline   Reply With Quote
Old 11-06-2005, 11:58 AM   PM User | #2
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Under that, a file such as blah.jpeg.html would show up.

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

Last edited by gsnedders; 11-06-2005 at 12:03 PM..
gsnedders is offline   Reply With Quote
Old 11-06-2005, 12:11 PM   PM User | #3
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
How about changing
PHP Code:
           for($i=0$i<sizeof($ext); $i++) 
               if(
stristr($file".".$ext[$i])) 
                                         echo 
"$file <br />"
to
PHP Code:
if(in_array(pathinfo($filePATHINFO_EXTENSION), $ext)
{
   print 
$file;

__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 11-06-2005, 06:45 PM   PM User | #4
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
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.
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Old 11-08-2005, 08:48 PM   PM User | #5
WA
Administrator


 
Join Date: Mar 2002
Posts: 2,596
Thanks: 2
Thanked 19 Times in 18 Posts
WA will become famous soon enough
I've just edited the code above to use regular expressions to better filter out a valid image based on its extension.
__________________
- George
- JavaScript Kit- JavaScript tutorials and 400+ scripts!
- JavaScript Reference- JavaScript reference you can relate to.
WA is offline   Reply With Quote
Old 11-21-2005, 08:42 PM   PM User | #6
shinko_metsuo
Regular Coder

 
Join Date: Sep 2004
Posts: 137
Thanks: 0
Thanked 0 Times in 0 Posts
shinko_metsuo is an unknown quantity at this point
How would I get it to list directories too?
shinko_metsuo is offline   Reply With Quote
Old 11-21-2005, 10:12 PM   PM User | #7
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
PHP Code:
//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.
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Old 11-21-2005, 10:25 PM   PM User | #8
shinko_metsuo
Regular Coder

 
Join Date: Sep 2004
Posts: 137
Thanks: 0
Thanked 0 Times in 0 Posts
shinko_metsuo is an unknown quantity at this point
Thanks a lot
shinko_metsuo is offline   Reply With Quote
Old 12-14-2005, 06:25 PM   PM User | #9
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Also

PHP Code:
"(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)" 
Can easily be
PHP Code:
"\.(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.
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:18 AM.


Advertisement
Log in to turn off these ads.