bmeadowcroft — 2011-09-27T15:42:28-04:00 — #1
I am having troubles with require_once() in my application.
So, I have two folders: 'lib' and 'content'. Both are at the same level in the folder structure (i.e. 'Application' > 'lib' & 'Application' > 'content').
Under the 'lib' folder, I have a couple others:
- lib > models
- lib > views
- lib > variables
Under my 'content' folder, I have the pages that are displayed in the browser (add.php, view.php).
On add.php, I have require_once('../lib/views/addClass.php');. When I run add.php in the browser, I get:
Warning: require_once(./variables/variables.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\application\lib\models\addClass.php on line 2
I can't figure out why it can't open the file. Any thoughts?
I am running XAMPP on Windows 7.
michael_morris1 — 2011-09-27T16:14:48-04:00 — #2
Relative paths are evil. I personally define the root path of my application as early as I can. That's probably what's going on here, check the path that PHP actually used to try to find the file and see if that's correct.
bmeadowcroft — 2011-09-27T16:31:10-04:00 — #3
Thanks for the response.
The funny thing is that I have a variables.php which I define my domain so that I can do something like require_once(DOMAIN . '/application/lib/views/add.php'); so I can avoid that but I have to pull in the variables.php file before I can use the constant.
How can I check the path that PHP used? Are you talking about "(include_path='.;C:\xampp\php\PEAR')"?
cups — 2011-09-28T04:57:45-04:00 — #4
You can carefully add the path
... to that ini directive, then restart the webservice (Apache?)
However, if you do not have access to your ini file on your LIVE server then you can alter this directive using Apache instructions (in .htaccess say), and even at the page level if you have one single boot file that every public pages is handled by.
Otherwise you will have to adopt another practice such as having a setting in a config file which contains the actual include path.
There are other ways.
webaddictz — 2011-09-28T07:10:33-04:00 — #5
Uhm, don't you mean set_include_path? I generally add a path like so:
__DIR__ . PATH_SEPARATOR . get_include_path( )
That works well for me.
cups — 2011-09-28T09:10:21-04:00 — #6
Nope, works as I expect ... what's the benefit of set_include_path then?
$a = ini_get('include_path');
echo 'some stuff <hr /> ';
$b = ini_get('include_path');
//string '.\\;\\var\\www\\includes;\\var\\www\\includes\\PEAR;' (length=77)
//some stuff <hr />
//string 'esra_ym' (length=7)
webaddictz — 2011-09-28T09:36:35-04:00 — #7
Hm, I guess there's more than one way to skin a cat in this instance. I didn't know your example actually would work. Another lesson learned