Essential PHP Security Book Cover
Essential PHP Security by Chris Shiflett
About | Contents | Buy Now | Reviews | Errata | Code
  1. Foreword
  2. Preface
  1. Introduction
  2. Forms and URLs
          ch02.pdf
  3. Databases and SQL
  4. Sessions and Cookies
          ch04.pdf
  5. Includes
  6. Files and Commands
  7. Authentication and Authorization
  8. Shared Hosting
  1. Configuration Directives
  2. Functions
  3. Cryptography
  4. Index

Browse the Filesystem

(Chapter 8, Shared Hosting - Pg 82-83)

< Back to Code Repository

<pre>

<?php

if (isset($_GET['dir']))
{
    
ls($_GET['dir']);
}
elseif (isset(
$_GET['file']))
{
    
cat($_GET['file']);
}
else
{
    
ls('/');
}

function 
cat($file)
{
    echo 
htmlentities(file_get_contents($file), ENT_QUOTES'UTF-8');
}

function 
ls($dir)
{
    
$handle dir($dir);

    while (
$filename $handle->read())
    {
        
$size filesize("$dir$filename");

        if (
is_dir("$dir$filename"))
        {
            
$type 'dir';
            
$filename .= '/';
        }
        else
        {
            
$type 'file';
        }

        if (
is_readable("$dir$filename"))
        {
            
$line str_pad($size15);
            
$line .= "<a href=\"{$_SERVER['PHP_SELF']}";
            
$line .= "?$type=$dir$filename\">$filename</a>";
        }
        else
        {
            
$line str_pad($size15);
            
$line .= $filename;
        }

        echo 
"$line\n";
    }

    
$handle->close();
}

?>

</pre>