The Sword of Pretty Thoughts

The (Creative) Life and Times of J.G. Jerome

Some Quick Gallery Code

At one time I wanted to show holiday party photos but didn’t want to build a full blown gallery site with plugins and what-not, so I hacked this together in about an hour:


// filename: index.php
//  purpose: a quick and dirty gallery for photos in a folder
 //parts inspired from

//Path to folder which contains images
$dirname = dirname("..");
//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob($dirname."/*.JPG");
   //echo dirname("..");

 echo "<!DOCTYPE html>\n";
 echo "<html>\n<head>\n";
 echo "<TITLE>Gallery Title in Browser Window goes here</TITLE>\n";
 echo "<center><h3>Title of Folder goes here</h3></center>\n";
 echo "</head>\n";
echo "\n\n\n"; 
echo "<body>\n";

 if ( !empty ( $images ) ) { 
	//Display image using foreach loop
	foreach($images as $image){
	//print the image to browser with anchor tag (Use if you want really )
	echo '<a href="'.$image.'" target="_blank"><img src="'.$image.'" style="width:100%; height:auto;" /></a>' . "\n";
	echo "No JPEG pictures found";
 echo "\n</body>\n</html>\n";

You’ll also need this in your .htacess file:

Options +Indexes

Zip file for the curious.

Photo credit:

Next Post

Previous Post

© 2019 The Sword of Pretty Thoughts

Theme by Anders Norén