Mostafa Talebi
Mostafa Talebi

Reputation: 9173

CodeIgniter Checking Files

I'm writing a template_loader for my CodeIgniter project. As usual, I need several security layers for my templates. One of them, which is the very first one, is checking if the files exists or not.

Now, My Problem is: I can't configure what address to give to the template loader. When I use simple php function called 'include()', it works, but with my template_loader function, it fails to work.

Here is my actual page (index.php):

<?php
    /**
    Add the page-suitable template from in folder.
    **/ 
    $this->template_loader->load_template('inc/temp_one_col.php');
    // include('inc/temp_one_col.php');

?>

And here is my class and template_loader:

class Template_loader 
{
    function load_template ($arg)
    {
        $base_name = basename($arg);

        $CI =&  get_instance();

        if(file_exists($arg) === true)
        {                       
                echo 'it is also good.';
                if (pathinfo($base_name, PATHINFO_EXTENSION) == 'php' ||
                pathinfo($base_name, PATHINFO_EXTENSION) == 'html'
                )
                {                   
                    $file_name_check = substr($base_name, 0, 4);
                    if($file_name_check === TEMP_FILE_INDICATOR)
                    {                       
                        include($arg);
                    }
                    else
                    {
                        redirect($CI->base_url . '/error/show_problem');                    
                    }
                }
                else
                {
                redirect($CI->base_url . '/error/show_problem');    

                }
        }
        else
        {
            redirect($CI->base_url . '/error/show_problem');            
        }       
    }
}

Upvotes: 1

Views: 86

Answers (1)

Jeemusu
Jeemusu

Reputation: 10533

Out of interest, what are you passing to the function as the $arg parameter?

Sounds like you just need to use the correct path to the file, which should be the absolute path to the file in the filesystem.

To get the absolute path you could create a new global variable in your sites index.php to point to your views folder.

webroot/index.php:

if (realpath('../application/views') !== FALSE)
{   
    $views_path =realpath('../application/views').'/';
    define('VIEWSPATH', $views_path);
}

Now you can use this as the base for your $arg parameter VIEWSPATH.$path_to_file

Upvotes: 2

Related Questions