Welcome Guest, Not a member yet? Register   Sign In
Static files controller
#1

[eluser]antonagestam[/eluser]
Hi there!

I'm quite new around here so please tell me if I do something wrong!

I've put together a controller that handles static files, like CSS, images and javascript files. There might already be some solution for this but the little resarch I did didn't find any. Please tell me if you find this useful!

You will need to redirect all requests to the 'static' folder to this controller. You also have to define the following variables in the config file:

Code:
// the folder where you keep your static files, defaults to 'static'
$config['static_files_dir'] = "static";
// defines in wich segment your static folder is. if you use mod_rewrite, it's probably 2
$config['static_files_starting_segment'] = 2;

Here is the controller:

Code:
<?php
    class Static_files extends Controller
    {
        
        public static $data = "";
        public static $file_name = "";
        
        public function __controller()
        {
        
            parent::Controller();
            $this->Index();
            
        }
        
        public function Index()
        {
            
            $path = $this->get_path();
            $file_status = $this->get_file($path);
            
            if( $file_status === true )
            {
                
                $this->set_output();
                
            }
            else
            {
                
                show_404();
                
            }
            
        }
        
        // @ Returns true on success or false as boolean on fail
        private function Get_file($full_path)
        {
            
            if( file_exists( $full_path ) )
            {
            
                $this->data = read_file( $full_path );
                $this->file_name = basename( $full_path );
                return true;
                
            }
            else
            {
                
                $this->data = (bool) false;
                $this->file_name = (bool) false;
                return false;
                
            }
            
        }
        
        private function Get_path()
        {
            
            $path = $this->config->item('static_files_dir') . "/";
            $starting_segment = $this->config->item('static_files_starting_segment');
            
            for( $int = $starting_segment;$int <= $this->uri->total_segments();$int ++ )
            {
                
                $path .= $this->uri->segment($int) . "/";
                
            }
            
            return trim( $path, "/" );
            
        }
        
        private function Set_output()
        {
            
            // Get mime type
            $mime = get_mime_by_extension( $this->file_name );
            
            // Set content-type header
            $this->output->set_header('Content-type: '.$mime);
            
            // Set output
            $this->output->set_output($this->data);
            
        }
        
    }


Messages In This Thread
Static files controller - by El Forum - 12-03-2009, 10:53 AM
Static files controller - by El Forum - 12-07-2009, 05:18 PM
Static files controller - by El Forum - 12-08-2009, 03:25 AM
Static files controller - by El Forum - 12-08-2009, 12:52 PM
Static files controller - by El Forum - 12-08-2009, 12:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB