我是一个完整的PHP菜鸟,我正在使用一个非常简单的PHP包括:
<?php include("~head.php"); ?>
Run Code Online (Sandbox Code Playgroud)
为网站做一些模板(为我的所有页面实现常见的页眉,页脚,菜单).
它适用于同一目录中的文件,但是当我在目录之外引用文件时,如下所示:
<?php include("../~head.php"); ?>
Run Code Online (Sandbox Code Playgroud)
但是,它似乎似乎没有找到文件,因为标题显然没有被拉入标记.
相反,如果我用完整的URL引用文件,例如
<?php include("http://example.com/~head.php"); ?>
Run Code Online (Sandbox Code Playgroud)
我在页面上收到以下错误代码.
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
Run Code Online (Sandbox Code Playgroud)
奇怪的是,"../ file.php"语法适用于非头文件(例如我用于菜单的包含).
因为这样的代码变得有点混乱,很难在所有不同的页面上保持变化.任何想法或解决方案将非常感谢.我真的是一个菜鸟,所以我可能无法将头脑包裹在任何太过花哨的地方.:)
谢谢你的时间.
乔恩
../这样的构造将创建完整的文件路径,而不是仅使用上面的目录来获取:
// Assuming you are including from the root
$application_path = dirname(__FILE__);
include("$application_path/../header.php);
Run Code Online (Sandbox Code Playgroud)
通常我会通过定义常量而不是使用变量来完成此操作.
define('APP_PATH', dirname(__FILE__));
Run Code Online (Sandbox Code Playgroud)
用它作为:
// 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");
Run Code Online (Sandbox Code Playgroud)