Home > Back-end >  get root folder in each php file
get root folder in each php file

Time:10-11

I have a include issue which I can't solve alone.

This is my folder structure:

enter image description here

for better explanation: There is a main folder "inc" (outside from accounting) and a subfolder inc (inset from accounting)

in the subfolder "inc" is a functions.php which has the following line:

require_once('../inc/tcpdf/tcpdf.php');

this works fine !

in the "ajax" folder I have the file task.php wit this line:

require '../inc/functions.php';

Here I get this error:

Warning: require_once(../inc/tcpdf/tcpdf.php): failed to open stream: No such file or directory in /volume1/web/accounting/inc/functions.php

Where is my mistake

CodePudding user response:

Don't use relative paths for include. Relative paths have the "current working directory" as a reference. The working directory is not fixed and can also be changed by code. Always use the constant

__DIR__

for your paths. This gives you an absolute path relative to the file where you write it:

require_once __DIR__.'/../inc/tcpdf/tcpdf.php';
  •  Tags:  
  • php
  • Related