Re: How to write a PHP coding to list out all files and directories aslinks to them?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/14/2011 6:16 PM, Mikhail S wrote:
How to write a PHP coding to list out all files and directories as links to
them?

This is somewhat similar to some index pages. When new file or folder is
added to the directory, HTML page should display the newly created
file/folder together with previous ones after it is being refreshed. (prefer
in alphabatical order)

How to achieve this sort of functionality in PHP? Please provide sample
coding as well. (and any references)

Thanks.

--
Mike
http://jp.shopsloop.com/


OK, maybe not the most elegant ... but it works for my purposes ... and it was my first attempt at these directory functions. You can navigate up and down the directory hierarchy. It flags files older than 1 year for potential cleanup.

Hope it gives you some ideas and a place to start...

Mitch
#! /usr/bin/php4
<html>
<head>
<title>Diskspace Analyzer</title>
<?php
foreach ($_REQUEST as $key=>$value){
	$$key = $value;
}
?>
</head>
<body>

<form name="form1" method="POST" action="<?php $PHP_SELF ?>">
  <p align="center"><font color="#FF0000" size="+2">Enter Directory Name Here</font></p>
  <p align="center"> 
    <input name="dName" type="text" id="dName" value="<?php $dName ?>" size="50" maxlength="256">
    <input type="submit" name="Submit" value="Submit">
  </p>
  </form>
<br>
<?php
if (isset($dName) && is_dir($dName)){
	print("<hr>\n");
	print("<font size=\"+2\"><strong>$dName Directory Information</strong></font><br>&nbsp;\n");
	print("<font color=\"yellow\"><strong>Highlighted</strong></font> files are older than 1 year and candidates for cleanup<br>&nbsp;\n");
	print("<table border=\"1\" bordercolor=\"gray\" cellspacing=\"0\" cellpadding=\"5\">\n");
	print("<th bgcolor=\"black\"><font color=\"white\">File Name</font></th><th bgcolor=\"black\"><font color=\"white\">File Size</font></th><th bgcolor=\"black\"><font color=\"white\">File Date</font></th>\n");
	$d = @dir(trim($dName));
	while ($entry = @$d->read()){
		$total++;
		$fileExt = explode(".", $entry);
		if(is_dir($d->path . "/" . $entry)){
			if($fileExt[0] ==""){
				$fileExt[1] = "SysDirectories";
			} else {
				$fileExt[1] = "Directories";
			}
			$dirSize = filesize($d->path . "/" . $entry);
			$totaldirSize += $dirSize;
			$dirList[$entry] = $dirSize;
		} else {
			$fileExt[1] = "Files";
			$fileStats = @stat($d->path . "/" . $entry);
			$fileSize = $fileStats[7];
			$totalSize += $fileSize;
			$currDate = getdate();
			$fdDay = date("z", $fileStats[10]);
			$fdYear = date("Y", $fileStats[10]);
			$ckDay = $currDate["yday"];
			$ckYear = ($currDate["year"] - 1);
			if (($fdYear < $ckYear) || (($fdYear == $ckYear) && ($fdDay <= $ckDay))){
				$highlight = "yellow";
				$totalSavings += $fileStats[7];
			} else {
				$highlight = "white";
			}
			print("\t<tr>\n");
			print("\t\t<td bgcolor=\"$highlight\">$entry</td><td bgcolor=\"$highlight\" align=\"right\">$fileSize</td><td align=\"right\" bgcolor=\"$highlight\">");
			printf("%s", date("m/d/Y", $fileStats[10]));
			print("</td>\n");
			print("\t</tr>\n");
		}
		$fileTypes[$fileExt[1]]++;
	}
	print("\t<tr>\n");
	print("\t\t<td bgcolor=\"gray\"><strong>Total Bytes in Files</strong></td><td align=\"right\" bgcolor=\"gray\"><strong>$totalSize</strong></td><td align=\"right\" bgcolor=\"gray\">");
	if ($totalSavings > 0){
		print("<strong>Potential Savings: $totalSavings</strong>");
	}
	print("</td>\n");
	print("\t</tr>\n");
	print("</table><br>&nbsp;\n");

	if(count($dirList) > 1){
		ksort($dirList);
		print("<table border=\"1\" bordercolor=\"gray\" cellspacing=\"0\" cellpadding=\"5\">\n");
		print("<th bgcolor=\"black\"><font color=\"white\">Directories</font></th>\n");
		foreach ($dirList as $key=>$value){
			print("\t<tr>\n");
			if ($key == "."){
				print("\t\t<td>$key &nbsp;(Current)</td>\n");
			} elseif ($key == ".."){
				$truncPoint = strrpos($dName, "/");
				$parentDir = substr($dName, 0, $truncPoint);
				if ($parentDir == ""){
					$parentDir = "/";
				}
				print("\t\t<td><a href=\"" . $PHP_SELF . "?dName=" . $parentDir . "&Submit=Submit\">$key</a> (Parent)</td>\n");
			} else {
				print("\t\t<td><a href=\"" . $PHP_SELF . "?dName=" . $dName . "/" . $key . "&Submit=Submit\">$key</a></td>\n");
			}
			print("\t</tr>\n");
		}
		print("\t<tr>\n");
		print("\t\t<td bgcolor=\"gray\"><strong>Total Bytes ($totaldirSize)</strong></td>\n");
		print("\t</tr>\n");
		print("</table><br>&nbsp;\n");
	}
	
	ksort($fileTypes);
	arsort($fileTypes);
	print("<ul>\n");
	foreach ($fileTypes as $key=>$value){
		print("<li>$key ($value)\n");
	};
	print("</ul>\n");
	print("Total Objects in $filespec branch: $total<br>\n");
	print("Total Bytes in $filespec branch: " . ($totalSize + $totaldirSize) . "<br>&nbsp;\n");
} else {
	Print ("<hr><br><h2><font color=\"#ff0000\">Requested Directory ( $dName ) was not found</font></h2>\n");
} // end if (isset(...
print ("<br><hr>\n");
?>
</body>
</html>
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux