include_once,php中的相对路径

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了include_once,php中的相对路径脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有3个文件:home,Failed_attempt,login.

文件home和Failed_attempt都是引用登录文件.

令人讨厌的是他们犯了一个错误,说登录文件不存在.如果我这样做,home会抛出一个异常,但是fail_attempt不会.

include_once("../StoredPRocedure/connect.PHP");

include_once( “../名字/ sanITize_string.PHP”);

如果我这样做:

include_once("StoredProcedure/connect.PHP");
   include_once("untitled/sanitize_string.PHP");

相反的情况发生,Failed_attempt抛出一个异常,但回家,不会.我该如何解决..

是否通过把这个../告诉include包括一个页面,因此home.PHP不需要去一页因此它会引发异常..

怎样才能这样做,以便两个文件都接受那些包含有效…也许不相对于它们的位置…即没有../

目录结构:

PoliticalForum
->home.PHP
->StoredProcedure/connect.PHP
->untitled/sanitize_string.PHP
->And other irrelevant files
这有三种可能的解决方案.第二种方法实际上就是以巧妙的方式使用绝对路径解决方法.

1:chdir进入正确的目录

<?PHP

// check if the 'StoredProcedure' folder exists in the current directory
// while it doesn't exist in the current directory,move current 
// directory up one level.
//
// This while loop will keep moving up the directory tree until the
// current directory contains the 'StoredProcedure' folder.
//
while (! file_exists('StoredProcedure') )
    chdir('..');

include_once "StoredProcedure/connect.PHP";
// ...
?>

请注意,只有当StoredProcedure文件夹位于可能需要包含其包含的文件的任何文件的最顶层目录中时,这才有效.

2:使用绝对路径

在您说这不可移植之前,它实际上取决于您如何实现它.这是一个适用于apache的示例:

<?PHP
include_once $_SERVER['DOCUMENT_ROOT'] . "/StoredProcedure/connect.PHP";
// ...
?>

或者,再次使用apache,将以下内容放在根目录中的.htaccess中:

PHP_value auto_prePEnd_file /path/to/example.PHP

然后在example.PHP中:

<?PHP

define('MY_DOC_ROOT','/path/to/docroot');

?>

最后在你的文件中:

<?PHP
include_once MY_DOC_ROOT . "/StoredProcedure/connect.PHP";
// ...
?>

3:设置PHP的include_path

请参阅the manual entry for the include_path directive.如果您无权访问PHP.ini,则可以在.htaccess中设置,如果您使用的是Apache而PHP未安装为CGI,如下所示:

PHP_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'

脚本宝典总结

以上是脚本宝典为你收集整理的include_once,php中的相对路径全部内容,希望文章能够帮你解决include_once,php中的相对路径所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。