开发者

Including a file from included file. Referencing issue

开发者 https://www.devze.com 2023-04-09 13:06 出处:网络
Here\'s a sample file structure: page.php db.php folder1/subpage.php folder2/fn.php page.php require_once(\'folder2/fn.php\');

Here's a sample file structure:

page.php

db.php

folder1/subpage.php

folder2/fn.php

page.php

require_once('folder2/fn.php');

folder1/subpage.php

require_once('../folder2/fn.php');

folder2/fn.php - requires database connection

require_once('../db.php');
/* then gets info from database */

Basically, fn.php contains database functions to get/set information. It requires db.php, which is database connection file. This code works in subpage.php, but not in page.php (gettin开发者_Python百科g "failed to open stream: No such file or directory in ..." error).

Is there a way (using relative referencing), so that it works in all files no matter of directory level?

Thanks for your help!


I always use the absolute path to the include. There are a couple ways to do this, this command will return the absolute path

<?php echo getcwd(); ?> 

But this is what I personally use: 
<? include $_SERVER['DOCUMENT_ROOT'] . "/includes/analytics.php"; ?> 

no matter where the file is php will have the absolute path.

0

精彩评论

暂无评论...
验证码 换一张
取 消