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' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |