how to include a file inside a model in codeigniter?

18,637

Solution 1

This should do:

base_url().'application/models/include/file.php'

(url helper must be loaded to use the base_url() function)

If you want a relative path:

./application/models/include/file.php

All files are relative to the main index.php, usually

Solution 2

Basically inserting direct file in model in not good approach. There are two best approach to follow... if you have class file then put your file in application/library folder and from model call like this..

$this->load->library('file name without .php');

Then use methods similar as you include file

include(classname.php);

If this situation does not suits you then make a file in helper ...make functions and then call from anywhere controller or model or view...

Hope this will help

Share:
18,637
gaggina
Author by

gaggina

Updated on July 11, 2022

Comments

  • gaggina
    gaggina almost 2 years

    I’m new to codeigniter and maybe my question is a bit stupid but..I cant understand how I can include a file inside a model.

    Inside del model folder I’ve a subfolder named include and inside this I’ve some php file.

    The structure is something like that:

    -models

    —mymodel.php

    —include —-file.php —-file1.php

    Inside mymodel.php how I can include the file.php?

    I’ve heard that the relative path is allways the index.php, so I’ve tried:

     include ‘models/include/file.php’
    

    but it seems not working.

    I’ve tried to use base_url() and site_url() without any luck..

    Can anyone help me?