Entry
I want to store the contents of a directory(.jpg files) in an array then echo the values. HELP!
Feb 24th, 2008 19:46
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.