在php中包含一次相对路径

在php中包含一次相对路径,php,file,include-path,Php,File,Include Path,我有3个文件:主页、尝试失败、登录 文件主目录和失败的\u尝试都指向登录文件 令人恼火的是,他们抛出了一个错误,说登录文件不存在。如果我这样做,home将抛出异常,但失败的尝试不会 include_once("../StoredProcedure/connect.php"); 包括_once(“../untitled/sanitize_string.php”) 如果我这样做: include_once("StoredProcedure/connect.php"); includ

我有3个文件:主页、尝试失败、登录

文件主目录和失败的\u尝试都指向登录文件

令人恼火的是,他们抛出了一个错误,说登录文件不存在。如果我这样做,home将抛出异常,但失败的尝试不会

  include_once("../StoredProcedure/connect.php");
包括_once(“../untitled/sanitize_string.php”)

如果我这样做:

   include_once("StoredProcedure/connect.php");
   include_once("untitled/sanitize_string.php");
相反的情况发生了,失败的尝试抛出一个异常,但是home不会。我该怎么解决这个问题

我是不是告诉include把这个../放到一个页面上去,因此home.php不需要往上一个页面,因此它会抛出异常

如何使两个文件都接受包含的文件为有效文件。。可能与它们的放置位置无关。。i、 e.没有这个/

目录结构:

PoliticalForum
->home.php
->StoredProcedure/connect.php
->untitled/sanitize_string.php
->And other irrelevant files

执行类似于
require(dirname(_文件).'[relativepath HERE]'的操作

在PHP中创建路径时,像
realpath()
\uuuu DIR\uuuu
之类的东西是您的朋友

Realpath

魔法常数

因为include和require是语言构造(而不是函数),所以它们不需要括号。一般来说,您会使用
include
来包含“模板”(输出文件)和
require
来包含PHP文件,例如类

因此,您可以使用以下内容:

$sPath = realpath( __DIR__ . '/../relative/path/here');
if($sPath) { require_once $sPath; }
(在PHP<5上使用
dirname(\uuuuu文件\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

如果文件不存在,则在尝试要求该文件之前评估路径是值得的,因为
realpath()
返回false,并尝试
要求false弹出一个PHP错误

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


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

其他人已经正确地回答了你的问题,但是我想我会贡献一些东西:自动加载器

自动加载程序允许您定义一个函数,当您尝试使用类时,该函数将自动包含某些文件

文档就在这里,可以比我更好地解释它:


我真的建议您使用它们,这将节省您的时间,而且是更好的实践。

这里有三种可能的解决方案。第二种方法实际上只是巧妙地使用绝对路径的变通方法

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";
// ...
?>
然后在example.php中:

<?php

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

?>

对于初学者来说,这将有助于理解。根据您的相对路径,只需使用以下选项:

//include file directly from parent directory:
include_once(dirname(__FILE__) . '/../connect.php');


你的意思是这样的吗:[code]include_once(dirname(FILE)。“./StoredProcedure/connect.php”);包括一次(dirname(FILE)。“./untitled/sanitize_string.php”)。。它不工作[/code]dirname(FILE)将返回包含代码的文件所在的当前目录,只需将相对路径附加到此文件即可。为方便起见,您可以打印dirname(文件)的值并查看发生了什么。但它不能完全正确地包括一次(dirname(文件)。“./StoredProcedure/User/login.php”);当我包含登录时,它会产生问题…您的代码行不适用于问题下的“daiscog”注释,并在问题中写入目录结构。已更新。如果目录给我我所在的目录。。因此,如果我在我的网站的“untitled”目录中,home.php在主目录中。。如果我把目录名放在相对路径之前,会有什么帮助呢?那也不行:include_once(realpath(“../untitled/sanitize_string.php”);它确实消除了错误。但如果realpath返回false呢?难道这不意味着文件不包括在内吗。。realpath何时返回false???
realpath()
如果作为参数输入的路径不存在,则返回false,例如
realpath('this/file/does/not/exist')
将返回false-如果文件确实存在,那么它将返回给定文件的真实路径,因此
realpath(_DIR__.'/this/file/does/exist')
可能会返回类似于
/home/mysite.com/htdocs/this/file/does/exist
-例如,完整的实际路径。我使用了尝试和捕获自动加载功能。我没有选择。不知道如何克服冲突。为什么绝对路径不起作用…像这样的政治论坛/无标题。。为什么该路径不能适用于所有文件,无论它们是什么..我讨厌相对路径另一种解决方法可能是将所有包含放在一个文件夹中,并使用类似于
set\u include\u path()
的内容将其设置为“包含路径”(尽管还有其他方法)。。。你的问题不是很清楚。主页、失败的\u尝试和登录文件是否在同一目录中?你能发布你的目录结构吗?主页和失败的_尝试页面如何“引用”登录文件?上面的代码出现在哪个文件中?StoredProcess是我的文件夹,里面有文件。一切如概述。。登录文件中有2个无法加载的包含项,取决于你从何处调用它们…主页或登录尝试页面位于不同的目录中我了解它们..当你开始页面时,文件被加载..你将你的包含放在其中..但这并不能解决我的问题是吗?我对第一种方法的工作原理有点不清楚。但是我会尝试第二和第三种方法。我已经在第一种方法中添加了注释。有关更多信息,请参阅。我的案例是第二种情况,这对我很有效。
<?php

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

?>
<?php
include_once MY_DOC_ROOT . "/StoredProcedure/connect.php";
// ...
?>
php_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'
//include file directly from parent directory:
include_once(dirname(__FILE__) . '/../connect.php');
//include file from parent's child directory:
include_once(dirname(__FILE__) . '/../StoredProcedure/connect.php');
//include file from own child directory:
include_once('StoredProcedure/connect.php');
//include sibling files from same directory:
include_once('connect.php');