获取PHP的问题包括使用不同文件夹中的文件 [英] Problem getting PHP include working with files in different folders
问题描述
我是一个完整的PHP菜鸟,我使用的是非常简单的PHP包括:
I'm a total PHP noob and am using a pretty simple PHP include:
<?php include("~head.php"); ?>
为网站做一些模板(为我的所有网站提供常见的页眉,页脚,菜单)页)。
to do a bit of templating for a website (to achieve common headers, footers, menus for all my pages).
- 注意:波形符(〜)只是为了让目录更易于查看(将主文件推送到顶部)按字母顺序排序的列表)
- NOTE: The tilde (~) is simply there to make the directories easier to look at (pushes main files to the top of the list when sorted alphabetically)
它适用于同一目录但我引用时的文件目录之外的文件,如下所示:
It's working great for files that are in the same directory but when I reference a file outside of a directory, like so:
<?php include("../~head.php"); ?>
然而,它似乎没有找到文件,因为标题显然没有被拉进入标记。
However, it simply doesn't seem to be finding the file as the header is clearly not being pulled into the markup.
相反,如果我用完整的网址引用该文件,例如
Conversely, if I reference the file with a full url, e.g.
<?php include("http://example.com/~head.php"); ?>
我的页面上出现以下错误代码。
I get the following error code on my page.
Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/content/65/7392565/html/bikini/angela_bikini.php on line 1
Warning: include(http://example.com/~head.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/content/65/7392565/html/products/product_a.php on line 1
Warning: include() [function.include]: Failed opening 'http://example.com/~head.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/65/7392565/html/products/product_a.php on line 1
奇怪的是,../ file.php语法适用于非头文件(例如我用于菜单的包含)。
Strangely, the "../file.php" syntax works for non-header files (e.g. the include I'm using for the menu).
因为这样的代码变得有点碎片,很难在所有不同的页面上维护变化。任何想法或解决方案将非常感谢。我真的是一个菜鸟,所以我可能无法绕过任何太过花哨的东西。 :)
As such code's gotten to be a bit of a fragmented mess and is difficult to maintain changes across all the different pages. Any thoughts or solutions would be very much appreciated. I really am a noob tho so I probably won't be able to wrap my head around anything too fancy. : )
感谢您的时间。
Jon
推荐答案
不是仅使用 ../
来获取上面的目录,这样的构造将创建完整的文件路径:
Rather than using only the ../
to get the directory above, a construct like this will create the full filepath:
// Assuming you are including from the root
$application_path = dirname(__FILE__);
include("$application_path/../header.php);
通常我' ll通过定义常量而不是使用变量来做到这一点。
Typically I'll do this by defining a constant, rather than using a variable.
define('APP_PATH', dirname(__FILE__));
将此用作:
// Assuming you are including at the file root:
define('APP_PATH', dirname(__FILE__));
include(APP_PATH . "/include/head.php");
// Assuming you are including from /include (one directory in)
// append a "/../" onto the end to indicate that the application
// root is one directory up from the currently executing file.
define('APP_PATH', dirname(__FILE__) . "/../");
include(APP_PATH . "somefile_at_the_root.php");
这篇关于获取PHP的问题包括使用不同文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!