PHP directory browsing/image display utility, Part 2

This is Part 2 of an earlier tutorial by the same name, “Part 1.”

I wrote this PHP5 utility as a quick and easy way to present design mockups to a reviewing party. It needed to be short and simple enough for a Designer to understand (and not freak out) and copy/paste. This script will read through a directory, either a default one, or one passed to the page via query string. It automatically reads every file in the given directory and displays the images found there. The Designer can name the files anything they want and they don’t have to edit any XML files.

This script will read through a directory, either a default one, or one passed to the page via query string. It automatically reads every file in the given directory and displays the images found there. The Designer can name the files anything they want and they don’t have to edit any XML files.

I chose to store the FolderBrowser class in a directory called com/timshaya/utils/, using the standard domain-based format. Here’s the code for FolderBrowser.php:

<?php		

	class FolderBrowser 
	{	
		private $myarr = array();
	
	    public function getArr() { return $this->myarr; }	
	
		public function __construct($vers)
		{ 
			($vers == null) ? $vers = "images" : $vers = $vers;

			$myDir = opendir($vers);		
			while (false !== ($file = readdir($myDir)))
			{
				if($file != "." && $file != ".." && $file != ".DS_Store")
				{
					array_push($this->myarr, $file); 	
				}
			}
			closedir($myDir);				
			
			$this->spitOutImgs($vers);
    	}
		
		public function spitOutImgs($vers)
		{			
			$ar = $this->getArr();
			for($i = 0; $i < count($ar); $i++)
			{	
				$pth = $vers."/".$ar[$i];
				$s = getimagesize($pth);						
				$c2 = "<div class='dv1'><img src='".$vers."/".$ar[$i];
				$c2 .= "' /><br />".$s[0]."x".$s[1]."</div>";	
				echo $c2; 	
			}
		}	
	}

?>

You can use the above class like so:

<?php 
    $version = htmlspecialchars($_GET["version"]); 
	require("com/timshaya/utils/FolderBrowser.php");
?>

<!-- <DOCTYPE... <html>...<body>... -->

<?php $myFolder = new FolderBrowser($version);	  ?>

<!-- ... </html>  -->

When I have some free time, I’ll continue this in Part 3. The next step would be to redesign FolderBrowser in such a way that it accounts for sub directories inside the main directory ($vers). I’ll probably create an interface or abstract class called AbstractFolderBrowser with basic functionality or method signatures. Then separate the logic currently in __construct() into a processDir() method. Each subclass of AbstractFolderBrowser will most likely override the processDir() method. So, SimpleFolderBrowser will do what’s above, with a few small changes, while SubDirFolderBrowser will be a bit more complex and accommodate sub directories, etc.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s