Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include file. Showing error file not found

Tags:

include

php

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

like image 305
kb0000 Avatar asked May 03 '26 08:05

kb0000


2 Answers

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.

like image 55
Shabbyrobe Avatar answered May 05 '26 22:05

Shabbyrobe


Use absolute path to include file:

$_SERVER['DOCUMENT_ROOT'].'/function/function.php'
like image 31
Pradeep Singh Avatar answered May 05 '26 22:05

Pradeep Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!