关于PHP中的文件包含方法

PHP开发时避免不了文件包含的问题,当文件不在同一个目录下面时文件如何包含是有些技巧及学问的,现在大致总结一下,希望对大家有所帮助。

几个测试文件的文件名及路径大致如下:

e:\myphp\research\path\path.php

e:\myphp\research\path\path1.php

e:\myphp\research\path\include\include.php

e:\myphp\research\path\include\path1.php



方法一:采用相对路径
path.php
<?php
define('NEWLINE', '<br />\n');
include_once('include/include1.php');
echo "This is file path.php\n";

path1.php
<?php
echo "This is file path1.php in root directory\n";
?>

include/include1.php
<?php
echo "This is file include1.php\n";
include_once('path1.php');
?>

include/path1.php
<?php
echo 'This is file path1.php in subdirectory include';
?>

执行path.php输出如下
This is file include1.php
This is file path1.php in root directory
This is file path.php

path目录下有文件path.php及path1.php,在path目录下面的include目录下有文件include1.php及path1.php
path.php包含include/include1.php,在include1.php中包含文件path1.php,由于在path目录及include目录下均有文件path1.php,则寻找path1.php的顺序是path,然后是path/include,php手册中也是这么说的

寻找包含文件的顺序先是在当前工作目录的相对的 include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 .,当前工作目录是 /www/,脚本中要 include 一个 include/a.php 并且在该文件中有一句 include "b.php",则寻找 b.php 的顺序先是 /www/,然后是 /www/include/。如果文件名以 ../ 开始,则只在当前工作目录相对的 include_path 下寻找。 

方法二:采用绝对路径
path.php
<?php
define('NEWLINE', '<br />\n');
include_once(dirname(__FILE__) . '/include/include1.php');
echo "This is file path.php\n";

path1.php
<?php
echo "This is file path1.php in root directory\n";
?>

include/include1.php
<?php
echo "This is file include1.php\n";
include_once(dirname(__FILE__) . 'path1.php');
?>

include/path1.php
<?php
echo 'This is file path1.php in subdirectory include';
?>

执行path.php输出如下:
This is file include1.php
This is file path1.php in subdirectory include
This is file path.php

由于在include/include1.php中采用了绝对路径,包含的是include子目录下面的path1.php,因此输出就不一样了。
推荐用这种方法,这样不管文件怎么包含,都不会出现找不到文件的情况,除非被包含的文件被删除了。

方法三:包含php.ini的include_path中指定的文件
例如php.ini中关于include_path的配置为
; Windows: "\path1;\path2"
include_path = ".;d:\myphp\research"
path.php
<?php
echo "This is file path.php\n";
include_once('ssi_inc.htm');
?>

path.php文件所在的目录下没有ssi_inc.htm文件,被包含的文件存在于e:\myphp\research目录下,由于此路径已经在php.ini中进行设置了,因此可以找到此文件。
E:\myphp\research\path>dir * | find "ssi_inc.htm"

E:\myphp\research>dir * | find "ssi_inc.htm"
2009/09/01  21:48                35 ssi_inc.htm

这种方法的缺点是当php的运行环境发生变化时必须重新修改php.ini的配置。

方法四:采用./及../
include/path1.php
<?php
echo "This is file path1.php in subdirectory include\n";
include_once('../path1.php'); 
?>

path1.php
<?php
echo "This is file path1.php in root directory\n";
?>

执行include/path1.php输出如下
This is file path1.php in subdirectory include
This is file path1.php in root directory

如果包含当前目录下的文件,采用./,其实跟方法一差不多的,这种方法的缺点是当根目录下的path1.php再次被其它目录下的文件包含时可以会出现找不到文件的错误,因此如果文件包含层次很多很深,不推荐用这种方法。

方法五.采用dirname结合./及../
path.php
<?php
echo "This is file path.php\n";
include_once(dirname(__FILE__) . './include/include1.php');
?>

include/include1.php
<?php
echo "This is file include1.php\n";
include_once(dirname(__FILE__) . '/../path1.php');
?>
include_once(dirname(__FILE__) . '/../path1.php');
用include_once(substr(dirname(__FILE__), 0, -7) . 'path1.php');也是可以的。

path1.php
<?php
echo "This is file path1.php in root directory\n";
?>

执行path.php输出如下:
This is file path.php
This is file include1.php
This is file path1.php in root directory
path.php包含include目录下的include1.php,include1.php再包含与path.php同目录下的path1.php文件,虽然用的./及../,但是仍然可以正确的找到文件,实际上采用的也是绝对路径。

注意include/include1.php中的包含路径必须用
include_once(dirname(__FILE__) . '/../path1.php');
而不能用include_once(dirname(__FILE__) . '../path1.php');
即..前面的/不能少,否则不能包含正确的文件

path.php中用
include_once(dirname(__FILE__) . './include/include1.php');
或include_once(dirname(__FILE__) . '/./include/include1.php');均可
即.前面有或没有/均可,均测试过。

Discuz论坛中的common.inc.php的包含文件也是采用类似的方式。
define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -7));
require_once DISCUZ_ROOT.'./config.inc.php';

方法六:采用自动加载文件
在php.ini在有如下的两项配置
; Automatically add files before or after any PHP document.
auto_prepend_file =
auto_append_file =
指定在主文件之前/后自动解析的文件名。为空表示禁用该特性。
该文件就像调用了include()函数被包含进来一样,因此会使用include_path指令的值。
注意:如果脚本通过exit()终止,那么自动后缀将不会发生。
发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览