2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by okparrothead
1

Hi,

What you want to do is:

Open a directory handle and read each element in the directory.
When you read a directory, it returns all the elements in the directory.
Some of these are files, and some folders and it will also return two "virtual" folders "." and "..".

When you read a directory element usign readdir(/* opendir handle */) it will return the element name, such as the file name, or the directory name.

What you need is to get the file name, and create a link with it.

Example:

Here's an example page that should do what you want. In the example all files in the subdirectory "imgs" will be read and displayed by the php script.

/<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show Images in Folder (simple gallery)</title>

<style type="text/css">

.img { 
	border: 1px solid #ccc;
	float: left;
	text-align: center;
	padding: 2px;
}

.img_label {
	border-bottom: 1px dotted #eee;
	display: block;
}

.sep {
	clear: both;
}

</style>

</head>

<body>

<?php

/**
* Read files in a directory
* @author digital-ether at fijiwebdesign.com
*
* @param string directory path
*/
function read_dir_elements($dir_path, $func) {
	if ($dh = opendir($dir_path)) {

		while ($el = readdir($dh)) {
			$path = $dir_path.'/'.$el;
	
			if (is_file($path)) {
				// execute callback function with the file path ($el) as a parameter
				call_user_func($func, $el);
			}

		}   
            
	   closedir($dh);

	} else {
		return false;
	}

	return true;
}

// example usage

$dir_path = 'imgs/';
$web_path = 'imgs/';

echo '<div class="gallery">';
read_dir_elements($dir_path, 'make_gallery_link');
echo '<div class="sep"></div>';
echo '</div>';

echo 'some more content';

// write the gallery link given the filename
function make_gallery_link($filename) {
	global $web_path;
	
	$img_name = preg_replace("/\.([^\.]*)$/", '', $filename);
	
	echo '<div class="img">';
	echo '<img src="'.$web_path.$filename.'" border="0" />';
	echo '<span class="img_label"><a href="'.$web_path.$filename.'">'.$img_name.'</a></span>';
	echo '</div>';
}

?>

</body>
</html>

Explanation:

The function below reads through all the files in a directory:

/**
* Read files in a directory
* @author digital-ether at fijiwebdesign.com
*
* @param string directory path
*/
function read_dir_elements($dir_path, $func) {
	if ($dh = opendir($dir_path)) {

		while ($el = readdir($dh)) {
			$path = $dir_path.'/'.$el;
	
			if (is_file($path)) {
				// execute callback function with the file path ($el) as a parameter
				call_user_func($func, $el);
			}

		}   
            
	   closedir($dh);

	} else {
		return false;
	}

	return true;
}

The bit of code:

if (is_file($path)) {
				// execute callback function with the file path ($el) as a parameter
				call_user_func($func, $el);
			}

checks if the element just read is a file. If it is, it then calls a callback function that you defined in read_dir_elements($dir_path, $func) as the second parameter.

I've created the function "make_gallery_link()" so that it can be called each time a file is read in the directory:

// write the gallery link given the filename
function make_gallery_link($filename) {
	global $web_path;
	
	$img_name = preg_replace("/\.([^\.]*)$/", '', $filename);
	
	echo '<div class="img">';
	echo '<img src="'.$web_path.$filename.'" border="0" />';
	echo '<span class="img_label"><a href="'.$web_path.$filename.'">'.$img_name.'</a></span>';
	echo '</div>';
}

This function outputs an image in the directory in the format:

<div class="img">
   <img src="imgs/icon_exclaim.gif" border="0" />
      <span class="img_label">
         <a href="imgs/icon_exclaim.gif">icon_exclaim</a>
      </span>
</div>

Note: I've just done this so that it is easy to style with CSS. It makes your php coding simpler also as you do not have to worry about the structure of the html output.

The main this is just that readdir($dh) returns a string, which is the name of the file or subdirectory in the directory being read. This string is then used to create the url and src to the img file.

Hope that helps.

;)

Votes + Comments
Very Helpful
0

digital-ether,

I've tried to create a script like this on more than one occasion, but no joy. Your instructions have finally gotten through my clumsy mind.

Thank you SO much.

Peace

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.