include_once,php中的相对路径

Mat*_*001 8 php file include-path

我有3个文件:home,failed_attempt,login.

文件home和failed_attempt都是指登录文件.

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

  include_once("../StoredProcedure/connect.php");
Run Code Online (Sandbox Code Playgroud)

include_once( "../名字/ sanitize_string.php");

如果我这样做:

   include_once("StoredProcedure/connect.php");
   include_once("untitled/sanitize_string.php");
Run Code Online (Sandbox Code Playgroud)

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

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

我怎样才能这样做,以便两个文件都接受那些包含有效...也许与它们放置的位置无关......即没有../

目录结构:

PoliticalForum
->home.php
->StoredProcedure/connect.php
->untitled/sanitize_string.php
->And other irrelevant files
Run Code Online (Sandbox Code Playgroud)

CD0*_*001 8

喜欢的东西realpath()__DIR__你的朋友们,当谈到在PHP创建路径.

Realpath http://php.net/manual/en/function.realpath.php

魔术常数 http://php.net/manual/en/language.constants.predefined.php

由于include和require是语言结构(而不​​是函数),因此它们不需要括号.一般来说,您可以使用include"模板"(输出文件)并require包含类文件等PHP文件.

所以你可以使用类似的东西:

$sPath = realpath( __DIR__ . '/../relative/path/here');
if($sPath) { require_once $sPath; }
Run Code Online (Sandbox Code Playgroud)

(使用dirname(__FILE__)而不是__DIR__PHP <5)

在尝试要求文件之前评估路径是值得的,realpath()如果文件不存在并尝试require false;解决PHP错误,则返回false .

或者你可以使用绝对路径,如:

require /home/www/mysite.com/htdocs/inc/somefile.php


meg*_*lop 8

这有三种可能的解决方案.第二种方法实际上只是以巧妙的方式使用绝对路径的解决方法.

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";
// ...
?>
Run Code Online (Sandbox Code Playgroud)

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

2:使用绝对路径

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

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/StoredProcedure/connect.php";
// ...
?>
Run Code Online (Sandbox Code Playgroud)

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

php_value auto_prepend_file /path/to/example.php
Run Code Online (Sandbox Code Playgroud)

然后在example.php中:

<?php

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

?>
Run Code Online (Sandbox Code Playgroud)

最后在你的文件中:

<?php
include_once MY_DOC_ROOT . "/StoredProcedure/connect.php";
// ...
?>
Run Code Online (Sandbox Code Playgroud)

3:设置PHP的include_path

请参阅include_path指令的手册条目.如果您无权访问php.ini,那么可以在.htaccess中设置,如果您使用的是Apache而PHP 安装为CGI,如下所示:

php_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'
Run Code Online (Sandbox Code Playgroud)


Rez*_*mun 5

对于初学者来说,这将有助于理解。根据您的相对路径简单地使用以下内容:

//include file directly from parent directory:
include_once(dirname(__FILE__) . '/../connect.php');
Run Code Online (Sandbox Code Playgroud)

或者

//include file from parent's child directory:
include_once(dirname(__FILE__) . '/../StoredProcedure/connect.php');
Run Code Online (Sandbox Code Playgroud)

或者

//include file from own child directory:
include_once('StoredProcedure/connect.php');
Run Code Online (Sandbox Code Playgroud)

或者

//include sibling files from same directory:
include_once('connect.php');
Run Code Online (Sandbox Code Playgroud)