faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 18 people (83%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

I want to store the contents of a directory(.jpg files) in an array then echo the values. HELP!

Nov 27th, 2008 09:01
healthy man, dman, Philip Olson, Dan Zambonini, John Himer, http://www.ttnr.org


Here's an example:
<?php
// Set $filedir to '.' if in current directory
$filedir = "/home/foobar/htdocs/test/uploads/";
$handle = opendir($filedir);
while ($filename = readdir($handle)) {
    // One way to get the file extension
    $ext = strtolower(substr(strrchr($filename, '.'),1));
    if ($ext == 'jpeg' || $ext == 'jpg') {
        // echo "<img src='$filename'>";
        $jpgs[] = $filename;
    }
}
closedir($handle);
?>
You asked for an array of jpg images, $jpgs will be an array of 
all .jpg and .jpeg images.  This of course can be expanded, you might 
want to replace:
  if ($ext == 'jpeg' || $ext == 'jpg') {
With:
  $goodtypes = array('gif', 'jpg', 'png', 'jpeg', 'bmp');
  if (in_array($ext, $goodtypes)) {
Makes it easy to add/manage additional types.  Uncomment the echo 
statement to echo it out immediatly, modify according to needs.
http://www.healthdothealth.com
http://www.healthxhealth.com
http://www.mesotheliomainmesothelioma.com
http://www.healthfirewall.com
http://www.flathealth.com
http://www.healthbodyguard.com