#native_company# #native_desc#
#native_cta#

PHP Filter

By Yulianta Ramelan
on January 29, 2003

Version: 1.1

Type: Full Script

Category: File Management

License: GNU General Public License

Description: PHP Filter is a program to display list of files in a directory which is filtered by it’s file extension. You can define your own filter by supplying the file extension and define icon representing the file by supplying your own hand created icon.

<?php
############################################################################
# 	index.php
#       By: Yulianta Ramelan<[email protected]>
#	This script is used for reading current directory contents
#	to create filtered list of files based on it's file extension
# What you have to do is:
# 1. Modify $filter variable with file extension you want to filter
# 2. Modify $icondir variable with the directory containing needed icons
# 3. Modify $icon variable with icon representing the coresponding file
# 4. Copy index.php to the directory you want to filter
############################################################################

//define filter with file extension, separate extensions with spaces
$filter = 'pdf txt';
//define directory (URL) containing the corresponding icons, no trailing slash
$icondir = '/icons';
//define icon representing the corresponding file, separate icons with spaces
$icon = 'acrobat.gif text.gif';


##################################################
# getdirattributes ($directory)
# function to get attributes of a directory
# no filter applied
##################################################
function getdirattributes ($directory)
{
	global $icondir;

	$modified = filemtime($directory);
	$strdate = date("d-M-Y", $modified);

	$html = "<tr>n";
	if (eregi("..", $directory))
	{
		$html .= "<td><img border="0" src="$icondir/back.gif" alt="[BACK]"></td>";
		$html .= "<td><a href="$path".rawurlencode($directory)."">Parent directory</a></td>";
	}
	else
	{
		$html .= "<td><img border="0" src="$icondir/folder.gif" alt="[DIR]"></td>";
		$html .= "<td><a href="$path".rawurlencode($directory)."">$directory</a></td>";
	}
	$html .= "<td>$strdate</td><td>&nbsp;</td>";
	$html .="</tr>n";

	return $html;
}


######################################################
# getfileattributes ($filteredfile)
# function to to check that the file match the filter
# and get the attributes
######################################################
function getfileattributes ($file)
{
	global $filter, $icondir, $icon;

	$kilobyte = 1024;
	$megabyte = 1024*1024;
	$filters = explode (" ", $filter);
	$icons = explode (" ", $icon);

	$modified = filemtime($file);
	$strdate = date("d-M-Y", $modified);

	$size = filesize($file);
	if ($size > $megabyte)
	{
		$size = $size/(1024*1024);
		$Strsize = eregi_replace("([0-9].[0-9])[0-9]*", "1MB", $size);
	}
	elseif (($size >= $kilobyte) && ($size < $megabyte))
	{
		$size = $size/1024;
		$strsize = eregi_replace("([0-9].[0-9])[0-9]*", "1KB", $size);
	}
	else
	{
		$strsize = $size."B";
	}

	for ($i=0; $i<=count($filters); $i++)
	{
		$currentfilter = $filters[$i];
		$currenticon = $icons[$i];
		if (eregi(".$currentfilter$", $file))
		{
			$html  = "<tr>n";
			$html .= "t<td><img border="0" src="$icondir/$currenticon" alt="[$currentfilter]"></td>";
			$html .= "<td><a href="".rawurlencode($file)."">$file</a></td>";
			$html .= "<td>$strdate</td><td align="right">$strsize</td>";
			$html .= "</tr>n";
		}
	}

	return $html;
}


######################################################
# function generatelist($dir)
# function to get the directory contents and
# grouping it into directory and file colletion
######################################################
function generatelist($dir)
{
	$dirhandle = opendir($dir);
	while ($file=readdir($dirhandle))
	{
		if (is_dir($file))
		{
			$dirs[] = $file;
		}
		else
		{
			if (eregi("^.$", $file) || eregi("^..$", $file) )
			{
				//get rid of current directory and parent directory
			}
			else
			{
				$files[] = $file;
			}
		}
	}

	sort ($dirs);
	sort ($files);

	for ($i=0; $i<=count($dirs)-1; $i++)
	{
		$html .= getdirattributes($dirs[$i]);
	}

	for ($i=0; $i<=count($files); $i++)
	{
		$html .= getfileattributes($files[$i]);
	}

	return $html;
}

$path = getenv('SCRIPT_NAME');
$path_array = explode('/', $path);
$script_name = $path_array[count($path_array)-1];
$path = eregi_replace("$script_name", '', $path);
?>

<html>
	<head>
		<title>
		Current directory :	<?php	echo $path; ?>
		</title>
	</head>
	<body>
		<h2>Current directory :	<?php echo $path; ?>
	<hr>
	<table>
<?php

	$currentdir= ".";
	$list = generatelist($currentdir);
	echo $list;
?>
	</table>
	</body>
</html>