lister.php

  1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2:             "http://www.w3.org/TR/html4/strict.dtd">
  3: <html lang="en"> <head>
  4: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5: <meta name="description" content="PHP File Lister">
  6: <meta name="author" content="Wayne Pollock">
  7: <link rel="contents" href="../index.htm">
  8: <link rel="previous" href="../index.htm">
  9: <link rel="Icon" type="image/x-icon" href="../images/PHP.ico">
 10: <link rel="stylesheet" href="../Styles.css" type="text/css">
 11: <script type="text/JavaScript" src="../Common.js"> </script>
 12: 
 13: <title> PHP File Lister (with optional line numbers) </title>
 14: 
 15: <style type="text/css">
 16: <!--
 17: @media screen {
 18: b  { color: yellow; }
 19: }
 20:  -->
 21: </style>
 22: 
 23: </head>
 24: <body>
 25: <div>
 26: <?php
 27:    // Script to list files, preserving white space,
 28:    // with optional line numbers.
 29:    //  Arguments:  file=path_to_file (NOT a URI, to prevent foreign
 30:    //                        documents)
 31:    //              linenums=true_or_false (default=false if omitted)
 32:    //              nodir=true_or_false (default=false if omitted)
 33:    //
 34:    // Written 2/2009 by Wayne Pollock, Tampa Florida USA.  All Rights Reserved.
 35: 
 36:    // Parse args: filename (string) and linenums (true or false):
 37:    if ( isset($_REQUEST['file']) )     $file = $_REQUEST['file'];
 38:    if ( isset($_REQUEST['linenums']) ) $linenums = $_REQUEST['linenums'];
 39:    if ( isset($_REQUEST['nodir']) )    $nodir = $_REQUEST['nodir'];
 40:    if ( isset($_REQUEST['dl']) )       $dl = $_REQUEST['dl'];
 41: 
 42:    if ( ! isset( $file ) ) {
 43:       echo "<h2>No filename provided!</h2>\n"
 44:         . "<p><em>You need to pass</em> "
 45:         . "<q><code>file=<em>name</em></code></q> in the query string.\n"
 46:         . "You can also add the <q><code>linenums</code></q> parameter "
 47:         . "to  display line numbers, the <q><code>nodir</code></q> "
 48:         . "parameter to truncate the heading to only show the filename, "
 49:         . "and the <q><code>dl</code></q> parameter to add a download link "
 50:         . "(useful when showing line numbers).</p>\n";
 51:       echo '</div></body></html>';
 52:       exit;
 53:    }
 54: 
 55:    if ( ! isset($dl) or $dl == "false" )
 56:       $dl = false;
 57:    else
 58:       $dl = true;
 59: 
 60:    if ( ! isset($linenums) or $linenums == "false" )
 61:       $linenums = false;
 62:    else
 63:       $linenums = true;
 64: 
 65:    // "nodir" means to only display the filename in the heading,
 66:    // and not the pathname.
 67:    if ( ! isset($nodir) or $nodir == "false" )
 68:       $nodir = false;
 69:    else
 70:       $nodir = true;
 71: 
 72:    // Output HTML document body (the file's contents):
 73:    if ( $nodir )
 74:       echo "<h1> " . basename($file) . " </h1>\n";
 75:    else
 76:       echo "<h1> $file </h1>\n";
 77: 
 78:    if ( $dl )
 79:       echo "<p class=\"Right\"><a href=\"$file\">Download "
 80:       . basename($file) . "</a></p>\n";
 81: 
 82:    echo "<pre>\n";
 83: 
 84: //   echo htmlentities( file_get_contents($file) );
 85:    $contents = file( $file, FILE_IGNORE_NEW_LINES );
 86:    $linenumwidth = strlen( count($contents) );
 87:    $format = "<b>%" . $linenumwidth . "d: </b>";
 88: 
 89:    foreach ($contents as $line_num => $line) {
 90:       if ( $linenums )
 91:          printf( $format, ($line_num + 1) );
 92:       echo htmlentities( rtrim($line) ) . "\n";
 93:    }
 94: 
 95:    // Output HTML epilog:
 96:    echo "</pre>\n";
 97: ?>
 98: </div>
 99: 
100: <div>
101: <script type="text/JavaScript">
102: // <![CDATA[
103:    addFooter( "Question: PHP File Lister" );
104: // ]]>
105: </script>
106: <noscript>
107:     <p> This page was last updated by Wayne Pollock. </p>
108: </noscript>
109: 
110: </div></body></html>