开发者

Include one PHP file into another

I need to include one PHP file into another. The PHP file that needs to be included sits in a separate directory though. This is how it is set up:

folder1/global-functions.php
folder1/folder2/functions.php

I need to include the 'global-functions.php' in the 'functions.php'

I tried:

<?php include("../global-functions.php"); ?>

But this will not work. It returns this error message:

Warning: include(../global-functions.php) [function.include]: failed to open stream: No such file or directory in /home/user/public_html/wp-content/themes/folder1/folder2/custom_functions.php on line 2

Warning: include() [function.include]: Failed opening '../global-functions.php' for inclusion (include_path='.:开发者_如何学运维/usr/lib/php:/usr/local/lib/php') in /home/user/public_html/wp-content/themes/folder1/folder2/custom_functions.php on line 2


Try including the file with an absolute path: something like this:

<?php include ($_SERVER['DOCUMENT_ROOT']."/folder1/global-functions.php");?>


Your original include fails because... the relative path in your include is relative to the current directory, which in your case is not "folder1/folder2/". The current directory is likely to be the page from which you are serving your content.

You need to either use an absolute path (with the help of $_SERVER['DOCUMENT_ROOT'] as in @Coomie's answer) or change your include_path to include the location of your included files (but then you must not use a relative path, but you wouldn't need to anyway).


You are including functions.php in itself. Change functions.php to global-functions.php.

And just out of curiosity, why have different files for functions? Why not make classes and objects and make your life easier?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜