3

I have a problem with my include statements. I have several php files that are loaded when the page is initially loaded and then called again by jQuery ajax calls. Because of the ajax calls I don't have the php script in a function and but I am getting an error with my includes statement when I call them from a different location (the ajax call). For example my main page includes this php script:

<div id="controlPanelForm">
    <?php include "lib/getControlPanel.php"; ?>
</div>

Here is the first few lines of the getControlPanel.php file:

<?php
include_once("../etc/includes.php");
...do something...

Here is the ajax call:

    function getControl(teamID)
{     $.ajax({
            type: "GET",
            data: {teamID: teamID},
            url: "./lib/getControlPanel.php",
            dataType: "html",
            async: true,
            success: function(response) 
            {
                $('#controlPanelForm').html(response);
            }
    });
}

So the problem is when I call the php script from the ajax call I get this error:

include_once(../etc/includes.php): failed to open stream: No such file or directory in 

If I change the relative path, the original include from the main page doesn't work...

How can I fix this issue?

Thanks in advance!

9
  • 1
    You probably need the file to be an absolute path. It's trying to include that file from wherever your jQuery file is being run. Commented Feb 8, 2013 at 22:22
  • I tried that but in my includes.php file I define a function and I get an error stating that I cannot re-declare a function. Commented Feb 8, 2013 at 22:27
  • 1
    Your file is being referenced incorrectly. Try hitting getControlPanel.php in your browser and you will get the same error. As a note, Kacey: it is not referencing the file relative to the file making the ajax call. The path is relative to the file that makes the include/require call. Commented Feb 8, 2013 at 22:27
  • That's what I was getting at. That's why I said change it to an absolute path, that way where ever it's called from it will load the file. Commented Feb 8, 2013 at 22:31
  • When I do an absolute path the initial include of the file from the main page works, but since I am using include_once the next time it is called from the ajax call it does not load. If I just do include I get this error on the initial include from the main page: CONSTANT HOST ALREADY DEFINED... Commented Feb 8, 2013 at 22:41

2 Answers 2

3
include_once(../etc/includes.php): failed to open stream: No such file or directory in

This error indicates that the file at '../etc/includes.php' doesn't exist. You can see your current working directory using getcwd() to determine where your relative links are actually pointing.

Relative paths, however, are a pain. I never use them. All of my projects contain a config.php file containing the configuration values:

// paths and urls
define('SITE_PATH', '/var/www/my_website/');
define('SITE_URL', 'http://localhost/my_website/');

Now everything can be absolute. Modifying your example:

<div id="controlPanelForm">
    <?php include SITE_PATH . 'lib/getControlPanel.php'; ?>
</div>

getControlPanel.php:

<?php
include_once(SITE_PATH . 'etc/includes.php');

Lots more can be done with these defines such as creating functions for loading files, sending redirect headers, etc.

On a side note, always test the ajax page by going to it directly using a web browser prior to debugging it with the AJAX call. That way you can tell whether the problem is a php problem or a javascript problem.

Sign up to request clarification or add additional context in comments.

4 Comments

Then wouldn't I run into the same issue with including the config.php file?
The config.php file is the only file you would need to know how to include. The rest you can be sure will be included using the correct path.
Use getcwd() to see your working directory. Then include 'config.php'; with the path from your working directory to your config.php.
If your still stuck read about includes, paths, and configuration files
0

You can fix it by correcting the path to your included file (includes.php). You can reference it asbolute or relative, doesn't matter. The important thing is that it is properly referenced.

Having said that, I prefer absolute paths (using constants to define paths up to web/app root).

To figure out the proper path (whether you want to use relative or absolute), drop a

echo phpinfo();

in your file and take a look at the DOCUMENT_ROOT or SCRIPT_FILENAME vars.

Comments

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.