Reputation: 762
It's been a while since I came across with this site. So far I've been viewing questions asked by different users and using the tips handed by the commenters in order to solve my issues and code problems. Sadly I couldn't manage to find a relative post to my current issue, hence I'm posting my very first help thread.
I'm using and 'include.php' file in my application which includes all the relevant files to my project. The file is located in the sub directory 'includes', therefore its path is 'includes/include.php'. Everything worked fine since I have started codding until my last decision to start using JQuery in my codes. Since I'm pretty new to both of these languages, I came across an annoying problem when trying to load a certain PHP file into one of my HTML divs. Instead of spending my time by verbally explaining to you what the problem is, I'll simply go ahead and show it to you.
In the top of my PHP files I use the next code in order to include my main include file:
require_once "/includes/include.php";
The content of my 'include' file is simply including the rest if the files, it goes like this:
require_once 'user.php';
require_once 'db.php';
Everything works fine with these files. Now I have a different include file, a script file, that I use in the middle of my code to post a certain HTML script. I use the next code in order to load it in my main PHP file:
<?php include '/includes/script_files/loadAdminMessageClass.php'; ?>
Everything works just fine so far as well. The problem starts when I try to refresh a certain div using JQuery and re-load the same 'loadAdminMessageClass' file to that div. This is the code line I use in order to reload the div:
$('#div_adminMessage').load('/includes/script_files/loadAdminMessageClass.php');
Here starts the problem. From this point onwards, the line 'require_once '/includes/include.php' in the first 'loadAdminMessageClass' doesn't work. The next error comes up in my div after the reload process:
Warning: require_once(/includes/include.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\includes\script_files\loadAdminMessageClass.php on line 8
Fatal error: require_once() [function.require]: Failed opening required '/includes/include.php' (include_path='.;C:\php5\pear') in C:\wamp\www\includes\script_files\loadAdminMessageClass.php on line 8
I've been searching this problem for ages and nothing was like the answer I was seeking for. Seems like when I call the 'loadAdminMessageClass.php' file from JQuery, it won't treat the 'require_once' call the way it should.
--
I'd be glad to have a response to this issue of mine. Thanks.
Upvotes: 1
Views: 612
Reputation: 791
You should try this one
require_once dirname(__FILE__) . '/includes/include.php';
Upvotes: 1
Reputation: 41958
I'm actually surprised this line works:
require_once "/includes/include.php";
This refers to the root of your server's file system. You're mistakenly assuming PHP respects the document root of your website when constructing include paths.
You have to explicitly retrieve $_SERVER['DOCUMENT_ROOT']
to find out where the website is running (something like /var/www/mywebsite
or /home/user/public_html
usually), and then construct the paths from there:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
require_once ROOT_PATH.'/includes/include.php';
Upvotes: 3