Log in

View Full Version : displaying all images in a dir



baconDelta
01-25-2012, 12:18 AM
so i found a bit of code to display all images in a chosen directory, it goes something like this:


<?php
$dir = '/r/';
$imgs = array();

if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (!is_dir($file) && preg_match("/\.(bmp|jpe?g|gif|png)$/", $file)) {
array_push($imgs, $file);
}
}

closedir($dh);
} else {
die('cannot open ' . $dir);
}

foreach ($imgs as $idx=>$img) {
$class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
echo '<img src="' . $dir . $img . '" alt="' .
$img . '"' . $class . ' />' . "\n";
}
?>


but i keep getting the 'cannot open' message. my images are in mywebsite.com/r/ so i set the dir as /r/ but i can't figure out why it's not able to open the dir. any ideas?

baconDelta
01-25-2012, 12:38 AM
well i figured out an alternative pretty quick, so i guess this thread is pretty useless now lol. since i store the urls in a db i used:


require_once("../functions/connection.php");

$result = mysql_query("SELECT * FROM store", $connection);
if(!$result){
die("query failed");
}
while($row = mysql_fetch_array($result)){
echo "<img src=\"http://www.mywebsite.com/".$row["url"]."\"><br />";
}
?>

seems like a better way. perhaps the best way? self resolved thread!