Reputation: 100331
I have this structure
controllers
- base.php
\ subfolder
- home.php
in home.php
I want to call
require("../base.php")
but it gives me an error message saying the file was not found. On the same folder it works.
Is there a way to get it working on the subfolder?
Upvotes: 0
Views: 850
Reputation: 13467
As of this writing, the accepted answer is correct in that require
(and similar function) paths are based on the location of index.php. However, CodeIgniter has very helpful constants defined in index.php that allow you to create paths to various parts of your application.
The APPPATH
constant would be perfect for pointing towards a controller/library/etc.
require APPPATH . 'controllers/base.php';
An even better solution would be to create a base controller in the form of application/core/MY_Controller.php
. You can then extend your controllers using MY_Controller
without the need to require any files. Check out Phil's article about Base Controllers.
Upvotes: 0
Reputation: 1683
CodeIgniter is executed from within index.php
script and paths of require
function are resolved based on index.php
path. For example if you would put test.php
in CodeIgniter root folder and then you would call require './test.php'
from your controller then the test.php
would be included without a problem.
To answer the question we need php magic constant __DIR__
which is always set to current script folder. The answer is:
require __DIR__."/../base.php";
called from within home.php
.
Edit: And platform independent solution would be:
require __DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."base.php";
Upvotes: 1
Reputation: 3022
Try:
require("/<full dir path>/base.php");
Another tip - sometimes in windows (not helpful if you plan to deploy on Linux) is sometimes you have to escape '\\' by using double slashes.
If that doesn't work, it is probably a permissions problem.
Edit: use this to get your include path:
echo ini_get('include_path');
That will tell you which path to use.
Upvotes: 1
Reputation: 4967
You can try to use full path:
$origin = $_SERVER['DOCUMENT_ROOT'];
$home = $origin. "/controllers/home.php";
require($home);
But I understand that It's a pain in the butt over a long term, but It's a solution. I have never had a problem like that before.
But maybe this could be of more help? PHP include file. Showing error file not found
Upvotes: 2