Location: PHPKode > scripts > File System > file-system/example.php
<?php
// +---------------------------------------------------------------+
// | File System Class Example Page                                |
// +---------------------------------------------------------------+
// | This example will allow you to list and sort files in the     |
// | specified directory. Change the ROOT_DIR constant below to    |
// | browse other directories, but be careful of what you allow to |
// | be seen. By using this script you agree that I take no        |
// | responsibility for site security (or lack thereof) whilst     |
// | using this script.                                            |
// +---------------------------------------------------------------+
// | Authors:       Jamie Curnow  <hide@address.com>                    |
// +---------------------------------------------------------------+

// NOTE:       This script is for PHP5 ONLY!
// More info:  http://www.gophp5.org/



    require_once('file_system.class.php');


    // This exmaple will search for items within this root dir.
    // Change it if you like, but make sure that if this script
    // is running on a live server, that you do not show any
    // important or valuable information.
    define('ROOT_DIR', '.');



    // For the purposes of this example we'll do all the logic
    // before printing anything to the output

    // Instantiate the Class Object
    $fs = new File_System();

    // Set base
    $directory = rtrim(ROOT_DIR, '/');
    $extended_dir = '';

    // Check for GET vars containing data, telling us what to get
    if (isset($_GET['dir'])) {
        // Because I believe in security, we don't want to allow
        // backwards relativity in the path, it's a security thing.
        if (strpos($_GET['dir'], '../') !== false) {
            die ('Stop trying to hack me!');
        }
        $extended_dir = rtrim($_GET['dir'], '/').'/';
    }
    $directory = $directory .= '/'.$extended_dir;

    $order = File_System::KEY_NAME;
    if (isset($_GET['order'])) {
        $order = $_GET['order'];
    }

    $direction = File_System::ASC;
    if (isset($_GET['direction'])) {
        $direction = $_GET['direction'];
    }



    // This class also supports a Callback functionality, where you
    // can filter the results of the listing with a custom function.
    // The callback must return an array with the same structure as
    // the listing.
    // We'll specify a function here:

    function customCallback($listing) {
        // This function will not show .htaccess files.
        $temp_array = array();
        foreach ($listing as $item) {
            if ($item[File_System::KEY_TYPE] == File_System::TYPE_DIR || ($item[File_System::KEY_TYPE] == File_System::TYPE_FILE && $item[File_System::KEY_NAME] != '.htaccess')) {
                $temp_array[] = $item;
            }
        }
        return $temp_array;
    }

    // Now assign the callback to the class:
    $fs->setFilterCallback('customCallback');



    // Get the Listing of the directory!
    $listing = $fs->getListing($directory, File_System::TYPE_BOTH, $order, $direction);

    // If you want to see the contents of the array, uncomment these lines:
    //print_r($listing);
    //exit();



    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>File System Class Examples</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<style>
            body {
                font-family:Arial;
                font-size:100%;
                margin:0;
                padding:10px;
            }

            .a_table {
                /*border:1px solid #000;*/
            }

            .a_table thead, .a_table tfoot {
                font-weight:bold;
                background-color:#98CF40;
                color:#555;
            }
            .a_table thead tr td a {
                text-decoration:none;
                color:#9F7645;
                display:block;
            }
            .a_table thead tr td a:hover {
                background-color:#BFA73B;
                color:#BF6A3B;
            }

            h3 {
                margin:10px;
                padding:0;
            }
    	</style>
    </head>

    <body id="body">
        <div id="wrapper">
            <div id="header">
                &nbsp;
            </div>
            <div id="main">

                <h3><?php print realpath($directory); ?></h3>

                <table border="0" width="100%" class="a_table" cellspacing="0" cellpadding="2">
                    <thead>
                        <tr>
                            <td width="16">
                                &nbsp;
                            </td>
                            <td>
                                <a href="<?php print $_SERVER['PHP_SELF']; ?>?dir=<?php print urlencode($extended_dir); ?>&order=<?php print File_System::KEY_NAME; ?>&direction=<?php
                                if ($order == File_System::KEY_NAME && $direction == File_System::ASC) {
                                    print File_System::DESC;
                                } else {
                                    print File_System::ASC;
                                }
                                ?>" title="Sort by Name">Name</a>
                            </td>
                            <td>
                                <a href="<?php print $_SERVER['PHP_SELF']; ?>?dir=<?php print urlencode($extended_dir); ?>&order=<?php print File_System::KEY_DATE; ?>&direction=<?php
                                if ($order == File_System::KEY_DATE && $direction == File_System::ASC) {
                                    print File_System::DESC;
                                } else {
                                    print File_System::ASC;
                                }
                                ?>" title="Sort by Date">Date</a>
                            </td>
                            <td>
                                <a href="<?php print $_SERVER['PHP_SELF']; ?>?dir=<?php print urlencode($extended_dir); ?>&order=<?php print File_System::KEY_SIZE; ?>&direction=<?php
                                if ($order == File_System::KEY_SIZE && $direction == File_System::ASC) {
                                    print File_System::DESC;
                                } else {
                                    print File_System::ASC;
                                }
                                ?>" title="Sort by Size">Size</a>
                            </td>
                            <td>
                                <a href="<?php print $_SERVER['PHP_SELF']; ?>?dir=<?php print urlencode($extended_dir); ?>&order=<?php print File_System::KEY_EXT; ?>&direction=<?php
                                if ($order == File_System::KEY_EXT && $direction == File_System::ASC) {
                                    print File_System::DESC;
                                } else {
                                    print File_System::ASC;
                                }
                                ?>" title="Sort by Type">Type</a>
                            </td>
                        </tr>
                    </thead>

                    <?php
                    foreach ($listing as $item) {
                        print '<tr>'."\n";

                        // Image
                        print '<td width="16"><img src="';
                        if ($item[File_System::KEY_TYPE] == File_System::TYPE_DIR) {
                            // Directory
                            print 'folder.png';
                        } else {
                            // File
                            print 'file.png';
                        }
                        print '" alt="Type" width="16" height="16" border="0" /></td>'."\n";

                        // Name
                        print '<td>';
                        if ($item[File_System::KEY_TYPE] == File_System::TYPE_DIR) {
                            print '<a href="'.$_SERVER['PHP_SELF'].'?dir='.urlencode($extended_dir.$item[File_System::KEY_NAME]).'&order='.$order.'&direction='.$direction.'" title="View Contents of '.htmlentities($item[File_System::KEY_NAME]).'">'.htmlentities($item[File_System::KEY_NAME]).'</a>';
                        } else {
                            print htmlentities($item[File_System::KEY_NAME]);
                        }
                        print '</td>'."\n";

                        // Date
                        print '<td>'.date('g:i a, jS F, Y', $item[File_System::KEY_DATE]).'</td>'."\n";

                        // Size
                        print '<td>'.getReadableSize($item[File_System::KEY_SIZE]).'</td>'."\n";

                        // Extension
                        print '<td>';
                        if (strlen($item[File_System::KEY_EXT]) > 0) {
                            print strtoupper($item[File_System::KEY_EXT]).' File';
                        } else {
                            print '&nbsp;';
                        }
                        print '</td>'."\n";

                        print '</tr>'."\n";
                    }
                    ?>
                    <tfoot>
                        <tr>
                            <td width="16">
                                &nbsp;
                            </td>
                            <td colspan="5">
                                <?php print $fs->getLastItemCount(); ?> file<?php print ($fs->getLastItemCount() == 1 ? '' : 's'); ?>,
                                <?php print getReadableSize($fs->getLastSize()); ?>
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>
            <div id="footer">
                &nbsp;
            </div>
        </div>
    </body>
    </html>
    <?php


    /**
     * getReadableSize
     * Returns the human readable representation of a file size
     *
     * @access public
     * @return string
     */
    function getReadableSize($bytes) {
        if ($bytes >= 1024) {
    		$size_kb = round(($bytes / 1024),0);
    		if ($size_kb >= 1024) {
    			$size_mb = round(($bytes / 1024 / 1024),2);
    			if ($size_mb >= 1024) {
    				$size_gb = round(($bytes / 1024 / 1024 / 1024),2);
    				$sizer = number_format($size_gb,2,".",",") . ' GB';
    			} else {
    				$sizer = number_format($size_mb,2,".",",") . ' mb';
    			}
    		} else {
    			$sizer = number_format($size_kb,0,".",",") . ' kb';
    		}
    	} else {
    		$sizer = $bytes . ' b';
    	}
    	return $sizer;
    }


?>
Return current item: File System