3

I am using include function and it is giving errors. That is file not found. In the root directory I have - index.php and config.php

index.php includes config.php and has some other data.

config.php has database details and includes function/function.php

There is a folder user and it has a file calculate.php in calculate.php I have included AJAX functionality and a file is loaded in it on AJAX call. File is cal2.php and it is located in user folder. Now this, cal2.php has a include function for config.php like:

include "../config.php";

When this cal2.php is loaded from calculate.php

function/function.php file is not loaded. It shows file not found for function/function.php

So, file structure is:

  • root
  • /index.php
  • /config.php
  • /user/calculate.php
  • /user/cal2.php
  • /function/function.php

How to proceed and not have function.php include error for cal2.php

5
  • how are you loading function/function.php, in cal2.php? please post that code. Commented Dec 19, 2010 at 9:12
  • 2
    2 rules to follow. 1. Always post actual code. 2. Always post exact and complete error message. Commented Dec 19, 2010 at 9:29
  • As for your problem - it's easy. Just use existing path. there is no /user/function/ folder in your system. Commented Dec 19, 2010 at 9:33
  • Actually, it isn't that easy. Following the instructions the OP has given for creating the filesystem to the letter and using 100% relative includes exactly as described does not cause a problem to occur. It works 100% a-ok without changing a single thing. Commented Dec 19, 2010 at 11:24
  • @Shabbyrobe the path can be relative or absolute, but first of all it should be correct, pointing to the existing file. So, the OP have to determine that path first. A static one. And only then start building it dynamically Commented Dec 19, 2010 at 13:50

3 Answers 3

7

You should change config.php to use an absolute path to functions.php. If you have PHP 5.3 or greater, do it like this:

include __DIR__.'/functions/functions.php';

If you are still using PHP 5.2 (or, god forbid, something earlier), you can do it this way:

$dir = dirname(__FILE__);
include $dir.'/functions/functions.php';

The __FILE__ magic constant always contains the value of the current PHP file name, so the dirname(__FILE__) call will give you the full filesystem path of the current script.

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

3 Comments

it will help include functions into config, but won't help include config into everything else. Don't use relative paths. use absolute ones.
__DIR__ and dirname(__FILE__) are absolute paths
It's still relative, silly. formally absolute, but it's built from the relative position from the current file. move that file somewhere else and your relative path would fail. That's why an absolute path should be always used, based on some constant.
2

Use absolute path to include file:

$_SERVER['DOCUMENT_ROOT'].'/function/function.php'

3 Comments

Using __DIR__ or dirname(__FILE__) is better as it is not dependent on the server environment in order to work.
@Shabbyrobe it isn't better. It won't help at all
This doesn't work when executing the php script from a command line (for example from cron).
-1

All problems here

/user/cal2.php
/function/function.php

you need to include such a way:

include('../config.php');

BUT I had this problem to, and I propose you to include files in the header of each page. Config you must inckude in the header of each page.

Read this, think it helps:

problems with global variables

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.