Task 3: Print a directory listing

Contents of /var/www/...:

iconnamesize  
  index.php 4KB
thumbnail nyphp.powered.logo.gif 4KB
  phplib dir
  phptal dir
thumbnail ramp.logo.gif 1KB
  smarty dir
  task1-plain.php 1KB
  task2-config.php 177B
  task2-plain.php 1KB
  task2-sql.txt 3KB
  task3-plain.php 2KB
  whynot dir
  whyyes.php 4KB

Index | Next»

Questions? csnyder@chxo.com

Source of this script

<?php
// initialize template
include_once('libs/Smarty.class.php');
$template= new Smarty;
$template->assign('title''Task 3: Print a directory listing');
$template->assign('next''index.php');

// TASK: display a directory listing with thumbnails for images and human-readable filesizes

// handy humansize function:
// input is number of bytes, output is a "human-readable" filesize string
function humansize($size) {
    
// only take numbers
    
if ( !is_numeric($size) ) return $size;
    
        
// Setup some common file size measurements.
    
$kb 1024;         // Kilobyte
    
$mb 1024 $kb;   // Megabyte
    
$gb 1024 $mb;   // Gigabyte
    
$tb 1024 $gb;   // Terabyte

        
if($size $kb) return $size."B";
    else if(
$size $mb) return round($size/$kb,0)."KB";
    else if(
$size $gb) return round($size/$mb,0)."MB";
    else if(
$size $tb) return round($size/$gb,0)."GB";
    else return 
round($size/$tb,2)."TB";
}
// register humansize as a custom modifier
$template->register_modifier('humansize','humansize');

// get local directory path
$pathdirname(dirname($_SERVER['SCRIPT_FILENAME']));
$template->assign('path'$path);

// get the directory contents into $files array
$d dir($path);
$files= array();
$index0;
while (
false !== ($entry $d->read())) {
    if ( 
substr($entry01)=='.' ) continue;
    
$files[]= $entry;
}
$d->close();

// natsort the entries
usort($files'strnatcmp');

// get sizes and extensions
$extensions= array();
$sizes= array();
foreach( 
$files AS $entry ) {
    
// get size
    
if ( is_dir($path.'/'.$entry) ) {
        
$sizes[]= 'dir';
    }
    else {
        
$sizes[]= filesize($path.'/'.$entry);
    }

    
// find filename extension
    
$dotposstrrpos($entry'.');
    if (
$dotpos) {
        
$extensions[]= substr($entry$dotpos+1);
    }
    else {
        
$extensions[]= '';
    }
}

// assign the files, extensions, and sizes arrays to the template
$template->assign('files'$files);
$template->assign('extensions'$extensions);
$template->assign('sizes'$sizes);

// display the template
$template->display('task3.tpl');
?>

<h3>Source of this script</h3>
<?php
$output
highlight_file($_SERVER['SCRIPT_FILENAME'],1);
print 
$output;
?>

<h3>Source of the template</h3>
<?php
$output
highlight_file(dirname($_SERVER['SCRIPT_FILENAME']).'/templates/task3.tpl',1);
print 
$output;
?>

Source of the template

{* Smarty: task 3 template *}
{include file="header.tpl"}

<h3>Contents of /var/www/...:</h3>
<table>
    <tr class="header">
        <td>icon</td><td>name</td><td>size &nbsp;</td>
    </tr>
{section name="f" loop=$files}
    <tr class="{cycle values="odd,even"}">
        <td>{if $extensions[f] eq "gif" or $extensions[f] eq "gif" or $extensions[f] eq "gif"}<img src="../{$files[f]}" alt="thumbnail" style="width: 48px; height: auto;" />{else}&nbsp;{/if}</td>
        <td><a href="../{$files[f]}">{$files[f]}</a></td>
        <td>{$sizes[f]|humansize}</td>
    </tr>
{sectionelse}
    <tr class="odd">
        <td colspan="3">There are no files here.</td>
    </tr>
{/section}
</table>

{include file="footer.tpl"}