Jump to content

I'm a newb - get, sort, and display images from a specified directory


chrisrushing

Recommended Posts

I'm trying to get the images in a directory and sort them numerically or alphabetically, whatever. And then just display them.

I'm still sooooo new at php so i'm doing a lot of tutorials/copy/pasting trying to figure things out.

I'm a designer at heart, please be nice.  :D

 

This is probably going to have to be completely rewritten to do so... It was from a tutorial so i copied/pasted most of it, and then edited what i needed.

It totally works as is and displays the images, it just doesn't put them in order.

 

I know I need to probably use sort($item,SORT_NUMERIC); but have no idea where to put it.

 

Either way, I'm not quite getting this... any help is appreciated.

 

<?php
$directory = 'images' . $row['imageName'];
	try {
		echo "<div class=\"next\"><div id=\"work\" class=\"pics\">";
		foreach ( new DirectoryIterator($directory) as $item ) {
			if ($item->isFile()) {
				$path = $directory . "/" . $item;
				echo "<img src=\"" . $path . "\" />";
			}
		}
	echo "</div></div>";
}
catch(Exception $e) {
	echo 'Whoops. Something did not work.';
}
?>

 

Try the follwing. It will list the files in the directory out and make them clickable to open/download. Replace directory with you actual directory name.

 

<?php

$dir="directory"; // Directory where files are stored

 

if ($dir_list = opendir($dir))

{

while(($filename = readdir($dir_list)) !== false)  //while there are files in the directory, do the following

{

?>

<p><a href="directory/<?=$filename?>"><?=$filename?></a></p>

<?php

}

closedir($dir_list);

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.