Location: PHPKode > scripts > File System > file-system/file_system.class.php
<?php
// +---------------------------------------------------------------+
// | File System Class                                             |
// +---------------------------------------------------------------+
// | Manages Files, Direcotires etc                                |
// +---------------------------------------------------------------+
// | Authors:       Jamie Curnow  <hide@address.com>                    |
// +---------------------------------------------------------------+

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


    class File_System {

        // Array Keys, also used to Sort By. You can't sort by type though.
        const KEY_TYPE            = 0;
        const KEY_NAME            = 1;
        const KEY_DATE            = 2;
        const KEY_SIZE            = 3;
        const KEY_EXT             = 4;

        // Sorting Directions
        const ASC                 = 1;
        const DESC                = 2;

        // TYpe Filters and Identifiers.
        const TYPE_BOTH           = 1;
        const TYPE_DIR            = 2;
        const TYPE_FILE           = 3;


        /**
         * Callback for File Type and Name Filters
         *
         * @var     object
         * @access  protected
         *
         **/
        protected $_filter_callback = false;

        /**
         * Stores the last directory listing total size in bytes
         *
         * @var     int
         * @access  protected
         *
         **/
        protected $_last_size = 0;

        /**
         * Stores the last directory listing total number of files
         *
         * @var     int
         * @access  protected
         *
         **/
        protected $_last_item_count = 0;




        /**
         * Constructor
         *
         * @access public
         * @return void
         */
        function __construct() {

        }


        /**
         * Sets the Filter Callback
         *
         * @access public
         * @return void
         */
        public function setFilterCallback($obj) {
            $this->_filter_callback = $obj;
        }


        /**
         * Order the results of a directory listing
         *
         * @access protected
         * @param  mixed  $array     The array of listing passed within getListing
         * @param  int    $type      The type of items to get (files, dirs, both). See constants above.
         * @param  int    $order     Which Key to order by
         * @param  int    $direction The direction to order by
         * @return array
         */
        protected function _sort($array, $type = File_System::TYPE_BOTH, $by = File_System::KEY_NAME, $direction = File_System::ASC) {
            $return_array = array();
            if (count($array) > 0) {
                // The Sorting
                $tmp_arr = array();
                foreach($array as $key => $value) {
                    $tmp_arr[$key] = $value[$by];
                }
                natcasesort($tmp_arr);
                if ($direction == File_System::DESC) {
                    $tmp_arr = array_reverse($tmp_arr, true);
                }
                foreach($tmp_arr as $key => $value) {
                    $return_array[] = $array[$key];
                }
                // If sorting by name, lets seperate the files from the dirs.
                if ($by == File_System::KEY_NAME && $type == File_System::TYPE_BOTH) {
                    $files = array();
                    $dirs  = array();
                    foreach ($return_array as $value) {
                        if ($value[File_System::KEY_TYPE] == File_System::TYPE_FILE) {
                            $files[] = $value;
                        } elseif ($value[File_System::KEY_TYPE] == File_System::TYPE_DIR) {
                            $dirs[] = $value;
                        }
                    }
                    if ($direction == File_System::DESC) {
                        $return_array = array_merge($files, $dirs);
                    } else {
                        $return_array = array_merge($dirs, $files);
                    }
                }
            }
            return $return_array;
        }


        /**
         * Return the listing of a directory
         *
         * @access public
         * @param  string $directory The Path to retrieve listing for
         * @param  int    $type      The type of items to get (files, dirs, both). See constants above.
         * @param  int    $order     Which Key to order by
         * @param  int    $direction The direction to order by
         * @return array
         */
        public function getListing($directory, $type = File_System::TYPE_BOTH, $order = File_System::KEY_NAME, $direction = File_System::ASC) {
            // Get the contents of the dir
    		$listing  = array();
            $directory = rtrim($directory,'/');

    		// Check Dir
    		if (!is_dir($directory)) {
    		    // You may want to include your own error handler here.
                die ($directory.' does not exist!');
    		}

    		// Get Raw Listing
    		$directory_handle = @opendir($directory);
            while (false !== ($file = @readdir($directory_handle))) {
                if (substr($file,0,1)!=".") {  #skip anything that starts with a '.' i.e.:('.', '..', or any hidden file)
                    // Directories
                    if (is_dir($directory.'/'.$file) && ($type == File_System::TYPE_BOTH || $type == File_System::TYPE_DIR)) {
                        $listing[] = array(
                            File_System::KEY_TYPE => File_System::TYPE_DIR,
                            File_System::KEY_NAME => $file,
                            File_System::KEY_DATE => filemtime($directory.'/'.$file),
                            File_System::KEY_SIZE => filesize($directory.'/'.$file),
                            File_System::KEY_EXT  => ''
                        );
                    // Files
    				} elseif (is_file($directory.'/'.$file) && ($type == File_System::TYPE_BOTH || $type == File_System::TYPE_FILE)) {
    					$listing[] = array(
                            File_System::KEY_TYPE => File_System::TYPE_FILE,
                            File_System::KEY_NAME => $file,
                            File_System::KEY_DATE => filemtime($directory.'/'.$file),
                            File_System::KEY_SIZE => filesize($directory.'/'.$file),
                            File_System::KEY_EXT  => $this->getExtension($file)
                        );
    				}
    			}
    		}
    		@closedir($directory_handle);

            // Sorting
            $listing = $this->_sort($listing, $type, $order, $direction);

            // Callbacks
            if ($this->_filter_callback) {
                $listing = call_user_func($this->_filter_callback, $listing);
            }

            // Total Size
            $total_size = 0;
            foreach ($listing as $item) {
                $total_size += $item[File_System::KEY_SIZE];
            }
            $this->_last_size = $total_size;

            // Item Count
            $this->_last_item_count = count($listing);

            // Done
            return $listing;
        }


        /**
         * Return the last listing size in bytes
         *
         * @access public
         * @return int
         */
        public function getLastSize() {
            return $this->_last_size;
        }


        /**
         * Return the last listing count of items
         *
         * @access public
         * @return int
         */
        public function getLastItemCount() {
            return $this->_last_item_count;
        }


        /**
         * getExtension
         * Returns the extension of a file, lowercase
         *
         * @access public
         * @return string
         */
        public function getExtension($file) {
            if (strpos($file, '.') !== false) {
                $tempext = strtolower(substr($file, strrpos($file,'.')+1,strlen($file)-strrpos($file,'.')));
                return strtolower(trim($tempext,'/'));
            }
            return '';
        }


        /**
         * Destructor
         *
         * @access public
         * @return void
         */
        function __destruct() {

        }




    }


Return current item: File System