Skip to content

Instantly share code, notes, and snippets.

@seungjin
Created May 17, 2009 00:58
Show Gist options
  • Save seungjin/112872 to your computer and use it in GitHub Desktop.
Save seungjin/112872 to your computer and use it in GitHub Desktop.
<?php
function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
$start_time = get_microtime();
// I do not use this function. Don't need it. Record this code just for reference
function getDirectorySize($path)
{
$totalsize = 0;
$totalcount = 0;
$dircount = 0;
if ($handle = opendir ($path))
{
while (false !== ($file = readdir($handle)))
{
$nextpath = $path . '/' . $file;
if ($file != '.' && $file != '..' && !is_link ($nextpath))
{
if (is_dir ($nextpath))
{
$dircount++;
$result = getDirectorySize($nextpath);
$totalsize += $result['size'];
$totalcount += $result['count'];
$dircount += $result['dircount'];
}
elseif (is_file ($nextpath))
{
$totalsize += filesize ($nextpath);
$totalcount++;
}
}
}
}
closedir($handle);
$total['size'] = $totalsize;
$total['count'] = $totalcount;
$total['dircount'] = $dircount;
return $total;
}
class PathToHTML {
var $html = "";
function __construct($path) {
$this->pathToHTML($path);
}
function byteConvert ( $bytes ) {
if ($bytes<=0) return '0 Byte';
$convention=1000; //[1000->10^x|1024->2^x]
$s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
$e=floor(log($bytes,$convention));
return round($bytes/pow($convention,$e),2).' '.$s[$e];
}
var $file_count = 0;
var $directory_count = 0;
var $volume = 0;
function pathToHTML($path) {
$handle = opendir($path);
$self_name = substr(__FILE__,strlen(getcwd())+1);
//read directory and store structure into array (for sorting)
$folder_arr = Array();
while ( $file = readdir($handle) ) {
is_dir($path."/".$file) ? $type="directory" : (is_file($path."/".$file) ? $type="file": $type="unknown" );
$size = filesize($path."/".$file);
$time = date("Y.m.d",fileatime($path."/".$file));
if ( ($file != "..") && ($file != "." ) && ($file != $self_name) ) {
$folder_arr[] = array( 'name' => $file , 'path' => $path , 'type' => $type , 'size' => $size , 'time' => $time);
}
}
//SORT
$name_arr = Array();
$path_arr = Array();
$type_arr = Array();
$size_arr = Array();
$time_arr = Array();
foreach ($folder_arr as $key => $row){
$name_arr[$key] = $row['name'];
$path_arr[$key] = $row['path'];
$type_arr[$key] = $row['type'];
$size_arr[$key] = $row['size'];
$time_arr[$key] = $row['time'];
}
array_multisort($time_arr, SORT_DESC, $name_arr, SORT_ASC, $type_arr, SORT_DESC, $size_arr, SORT_DESC, $folder_arr);
//list folder structure array
foreach ( $folder_arr as $file ) {
if ( $file['type'] == "file" ) {
$this->file_count++;
$this->volume += $file['size'];
$web_path = substr($path,strlen(getcwd())+1);
$this->html = $this->html . "<li type='disc'><a href='./".$web_path."/".htmlspecialchars($file['name'], ENT_QUOTES)."'>".htmlspecialchars($file['name'], ENT_QUOTES)."</a> (".$this->byteConvert($file['size']).", ".$file['time'].")</li>\n";
}
if ( $file['type'] == "directory" ) {
$this->directory_count++;
$this->html = $this->html . "<li type='square'>".$file['name']." (".$file['time'].")</li>\n<ul>\n";
$this->pathToHTML($file['path']."/".$file['name']);
$this->html = $this->html ."</ul>\n";
}
}
closedir($handle);
}
}
class PathToXML {
var $xml = "";
function __construct($path) {
$this->pathToXML($path);
}
function byteConvert ( $bytes ) {
if ($bytes<=0) return '0 Byte';
$convention=1000; //[1000->10^x|1024->2^x]
$s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
$e=floor(log($bytes,$convention));
return round($bytes/pow($convention,$e),2).' '.$s[$e];
}
function pathToXML($path) {
$handle = opendir($path) ;
$self_name = substr(__FILE__,strlen(getcwd())+1);
while ( $file = readdir($handle) ) {
if ( ($file != "..") && ($file != ".") && ($file != $self_name) ) {
is_dir($path."/".$file) ? $type="directory" : (is_file($path."/".$file) ? $type="file" : $type="unknown" );
$size = $this->byteConvert(filesize($path."/".$file));
$time = date("Y.m.d H:i",fileatime($path."/".$file));
if ( $type == "file" ) {
$web_path = substr($path,strlen(getcwd())+1);
$this->xml = $this->xml . "<file type='file' name='$file' size='$size' time='$time' />\n";
}
if ( $type == "directory" ) {
$this->xml = $this->xml . "<file type='directory' name='$file' time='$time' >\n";
$this->pathToXML($path."/".$file);
$this->xml = $this->xml ."</file>\n";
}
}
}
closedir($handle);
}
}
class FilesXmlToHtml {
var $html = "";
function __construct($xml) {
$this->toHTML($xml);
}
function toHTML($givenXML) {
print($givenXML);
//$doc = new DOMDocument();
//$doc->load($givenXML);
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SEUNG-JIN KIM</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<meta name="ROBOTS" content="NOINDEX">
<!-- Copyright (C) 2006-<?php echo date("Y"); ?> seungjin.net -->
<meta name="date" content="<?php echo date("D M j G:i:s T Y"); ?>">
<meta name="author" content="Kim, Seung-jin">
<style type="text/css">
<!--
/* white.css */
body
{
color: #333333;
font-family: 84.5% Georgia, Batang, Georgia, Times New Roman, Times, Sans Serif;
font-size: 12px;
background-color: white;
font: 돋움, 84.5% Georgia, Batang, Georgia, Times New Roman, Times, Sans Serif;
margin-top:10px;
margin-bottom:10px;
margin-left:10px;
margin-right:10px;
}
TD, TH, P, B
{
color: #333333;
font-family: 84.5% Georgia, Batang, Georgia, Times New Roman, Times, Sans Serif;
font-size: 12px;
line-height: 135%;
}
ul
{
list-style:none;
padding-left:30px;
text-indent:0oem;
}
p { margin-top: 6px; margin-bottom: 7px; }
a:link { color: #004276; text-decoration:none }
a:active { color: #004276; text-decoration:none }
a:visited { color: #666699; text-decoration:none }
a:hover { color: #CCCCCC; text-decoration:underline }
IMG {border: none;}
/* end of white.css */
-->
</style>
</HEAD>
<body>
<hr/>
<?php
$pathToHTML = new PathToHTML(getcwd());
print("<ul>\n".$pathToHTML->html."\n</ul>");
print("$pathToHTML->file_count file{,s} with $pathToHTML->directory_count director{y,ies}. Volume size: ".$pathToHTML->byteConvert($pathToHTML->volume).". Scaned at ".date("D M j G:i:s T Y"));
?>
<hr />
• Prg. by Seung-jin Kim • http://www.seungjin.net • v.20081208_0
</body>
</html>
<!-- xml / FOR RIA -->
<?php
$pathToXML = new PathToXML(getcwd());
print("<seungjin>\n".$pathToXML->xml."</seungjin>");
?>
<?php
$page_time = round((get_microtime() - $start_time), 4);
print("\n\n<!-- Page generated in '$page_time' seconds.-->");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment