Skip to content

simple file browser

Derek Jones edited this page Jul 4, 2012 · 14 revisions

Feel free to improve it here, but PLEASE fill the 'notes' field below to explain your contribution.

[h3]documentation[/h3] An url like http://www.your-site.com/index.php/browser/[b]virtual_root[/b]/[b]path[/b] will allow people to browse directories and files that are under [b]path[/b] from [b]absolute_path[/b]. You define an [b]absolute_path[/b] (in the file system of the server) for a [b]virtual_root[/b] with this array

var $roots = array(
    'virtual_root' => 'absolute_path',
    //...
    );
   

This may allow us to browse files that are outside your web root, forcing the browsing via CI, and allowing us full control on accesses (not provided for the moment).

[h3]todo[/h3]

  • stats for directories and files
  • links for sorting directories and files
  • clever css markup
  • several examples of stylesheets
  • bells and hooks and whistles and filters to check accesses
  • ...

[h3]changes[/h3] 2009-10-22 - annoted in-code, line 98 of controller, '.$absolute.' changed to $absolute

[h3]the controller[/h3]

<?php

// may only work if this controller is not in a subfolder

class Browser extends Controller {

    var $roots = array(
        'test' => '/home'
        );
    
    function Browser()
    {
        parent::Controller();
        // FAL check here
    }
    
    function _remap()
    {
        $segment_array = $this->uri->segment_array();
        
        // first and second segments are our controller and the 'virtual root'
        $controller = array_shift( $segment_array );
        $virtual_root = array_shift( $segment_array );
        
        if( empty( $this->roots )) exit( 'no roots defined' );
        
        // let's check if a virtual root is choosen
        // if this controller is the default controller, first segment is 'index'
        if ( $controller == 'index' OR $virtual_root == '' ) show_404();
        
        // let's check if a virtual root matches
        if ( ! array_key_exists( $virtual_root, $this->roots )) show_404();
        
        // build absolute path
        $path_in_url = '';
        foreach ( $segment_array as $segment ) $path_in_url.= $segment.'/';
        $absolute_path = $this->roots[ $virtual_root ].'/'.$path_in_url;
        $absolute_path = rtrim( $absolute_path ,'/' );
        
        // is it a directory or a file ?
        if ( is_dir( $absolute_path ))
        {
            // we'll need this to build links
            $this->load->helper('url');
            
            $dirs = array();
            $files = array();
            // let's traverse the directory
            if ( $handle = @opendir( $absolute_path ))
            {
                while ( false !== ($file = readdir( $handle )))
                {
                    if (( $file != "." AND $file != ".." ))
                    {
                        if ( is_dir( $absolute_path.'/'.$file ))
                        {
                            $dirs[]['name'] = $file;
                        }
                        else
                        {
                            $files[]['name'] = $file;
                        }
                    }
                }
                closedir( $handle );
                sort( $dirs );
                sort( $files );
                
                
            }
            // parent directory
            // here to ensure it's available and the first in the array
            if ( $path_in_url != '' )
                array_unshift ( $dirs, array( 'name' => '..' ));
            
            // send the view
            $data = array(
                'controller' => $controller,
                'virtual_root' => $virtual_root,
                'path_in_url' => $path_in_url,
                'dirs' => $dirs,
                'files' => $files,
                );
            $this->load->view( 'browser', $data );
        }
        else
        {
            // it's not a directory, but is it a file ?
            if ( is_file( $absolute_path ))
            {
                // let's serve the file
                header ('Cache-Control: no-store, no-cache, must-revalidate');
                header ('Cache-Control: pre-check=0, post-check=0, max-age=0');
                header ('Pragma: no-cache');
                 
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                // header('Content-Length: ' . filesize( ".$absolute_path." ));  // Modified
                header('Content-Length: ' . filesize( $absolute_path ));
                header('Content-Disposition: attachment; filename=' . basename( $absolute_path ));
                
                @readfile( $absolute_path );
            }
            else
            {
                show_404();
            }
        }
    }
}

[h3]the view[/h3]

<h1>&lt;?=$virtual_root?&gt;</h1>
<h2>&lt;?='/'.$path_in_url?&gt;</h2>
&lt;?php
    $prefix = $controller.'/'.$virtual_root.'/'.$path_in_url;
    if (!empty($dirs)) foreach( $dirs as $dir )
        echo 'd '.anchor($prefix.$dir['name'], $dir['name']).'<br>';

    if (!empty($files)) foreach( $files as $file )
        echo anchor($prefix.$file['name'], $file['name']).'<br>';
?&gt;

Clone this wiki locally