Location: PHPKode > scripts > Image directory index with BBCode generation > image-directory-index-with-bbcode-generation/index.php
<?php
/*
photo_index.php
-----------------------------------------------------------------------------------------------------
This is a very simple script to generate a 'preview' of a directory of images.  It will ignore
any files which aren't .gif, .jpg, .jpeg, or .png.  Basically it will generate an HTML page with
a textbox of BBCode (for posting on forums) and the <img> itself so you can preview it.  Saves
a lot of time when you would normally be looking at a regular directory index and clicking back
and forth!

I recommend you place this file in your favorite includes/ folder or the root of your website, and
call it using the php include() function.  That way should you find a need to modify this file, it's
centrally located in one spot. ;)

** WHATEVER YOU DO...
** This file (or the file which will include() this file) needs to be named index.php
** Then you place the index.php file in the directory you wish to view.
** That's really it.

I wrote this on a whim so feel free to clean it up, change it, distribute, whatever.

Email me if you have any questions or suggestions... webguy at 330i dot net

-Chris
-----------------------------------------------------------------------------------------------------
*/

// modify this to reflect your server's domain name.
// NOTE: format is protocol://domain_name with NO trailing slash.
$root_server_url = "http://images.330i.net";

// ** ** ** **  	No need to modify beyond this line  	** ** ** **

// if the request wasn't for the index.php page directly, redirect the client
if(!strpos($_SERVER['REQUEST_URI'], "index.php")){
	header("location: index.php");
}

// for each file in the directory...
$dir_name = "./";
$myfiles=GetDirContents(dirname($dir_name));

for($i=0; $i<count($myfiles); $i++){
	$num = $i+1;								// file number increment
	$stroutput = showFile($myfiles[$i]);		// either get formatted output for the file, or a false
	echo "<b>" . $num . ".</b> ";

	if($stroutput){
		echo $stroutput;
	}else{
		echo "File skipped; not gif, png nor jpg.";
	}
}
?>

<?php
// GetDirContents(mapped_directory)
// Thanks to andy.at.azengel.dot.com for this function, found at
// http://php.mirrors.ilisys.com.au/manual/en/ref.dir.php
//
// This function returns an array of filenames (with the directory appended) which we
// use in a loop.
function GetDirContents($dir){
	ini_set("max_execution_time", 10);
	if (!is_dir($dir)){die ("Error!");}												// is it a directory?
	if ($root=@opendir($dir)){
		while ($file=readdir($root)){
			if($file=="." || $file==".."){continue;}
			if(is_dir($dir."/".$file)){
				$files=array_merge($files,GetDirContents($dir."/".$file));
			}else{
				$files[]=$dir."/".$file;
			}
		}
	}

	return $files;		// array of dir+filenames
}

// showfile(filename)
// returns HTML to be output'd to the client
// (alter to suit your needs)
function showFile($filename){
	if(strpos(strtolower($filename), ".gif") || strpos(strtolower($filename), ".jpg" || strpos(strtolower($filename), ".jpeg") || strpos(strtolower($filename), ".png"){
		$filename = makeurl($filename);
		return "<input type='text' style='width: 600px; font-size: 8pt;' value='[img]" . $filename . "[/img]' onClick='this.select();'><br><img style='margin-top: 5px;' src='" . $filename . "'><hr size=1><br>";
	}else{
		return false;
	}
}

// changes our relative pathing to a fully qualified URL for bbcode purposes
function makeurl($relative_path) {
	global $dir_name, $root_server_url;
	return $root_server_url . str_replace("index.php", str_replace($dir_name, "", $relative_path), $_SERVER["REQUEST_URI"]);
}
?>
Return current item: Image directory index with BBCode generation