1

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.

3
  • 6
    Your path to your include file is obviously wrong. Look into using __dir__ to help you get it right. Commented May 31, 2013 at 13:08
  • 2
    I don't know how the error message could be any more explicit about what is wrong. Commented May 31, 2013 at 13:09
  • The path was not set correctly. It has been solved though, thanks. Commented May 31, 2013 at 13:30

2 Answers 2

3

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';
Sign up to request clarification or add additional context in comments.

1 Comment

That was indeed the solution for me. Thank you very much.
1

You should try this one

require_once dirname(__FILE__) . '/includes/include.php';

1 Comment

Since PHP5.3 you can use __DIR__ instead of dirname(__FILE__).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.