【问题标题】:include_once, relative path in phpinclude_once,php中的相对路径
【发布时间】:2011-12-11 18:22:23
【问题描述】:

我有 3 个文件:home、failed_attempt、login。

文件 home 和 failed_attempt 都指向登录文件。

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

  include_once("../StoredProcedure/connect.php");

include_once("../untitled/sanitize_string.php");

如果我这样做:

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

相反的情况发生了, failed_attempt 抛出异常,但是 home 不会。我该如何解决这个问题..

我是否通过放置这个 ../ 来告诉包含上一页,因此 home.php 不需要上一页,因此它会引发该异常..

我怎样才能使这两个文件都接受这些包含为有效..可能与它们放置的位置无关..即没有那个../

目录结构:

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

【问题讨论】:

  • 你的问题不是很清楚。 home、failed_attempt 和 login 文件是否在同一个目录中?你能发布你的目录结构吗? home 和 failed_attempt 页面究竟是如何“引用”登录文件的?以上代码出现在哪个文件中?
  • storedProcedure 是我的文件夹,里面有文件。概述的所有内容..登录文件中有2个无法加载的包含,具体取决于您从何处调用它们...主页或位于不同目录中的login_attempt页面

标签: php file include-path


【解决方案1】:

在 PHP 中创建路径时,realpath()__DIR__ 是您的朋友。

真实路径 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; }

(在 PHP dirname(__FILE__) 而不是 __DIR__

在尝试请求文件之前评估路径是值得的,因为如果文件不存在,realpath() 会返回 false,并且尝试 require false; 会发出 PHP 错误。

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

require /home/www/mysite.com/htdocs/inc/somefile.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 的内容 - 例如完整的实际路径。
  • 我在 __autoload 函数中使用了尝试和捕获。我别无选择 ..不知道如何克服这种冲突。为什么绝对路径不起作用......就像这个政治论坛/无标题......为什么该路径不能适用于所有文件,无论它们是什么......我讨厌相对路径
  • 解决它的另一种方法可能是将所有包含在一个文件夹中,并使用set_include_path() 之类的东西将其设置为“包含路径”(虽然还有其他方法)... @ 987654323@
【解决方案2】:

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

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 not 安装为 CGI,如下所示:

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

【讨论】:

  • 我对第一种方法的工作原理有点模糊。但我会尝试第二种和第三种方法
  • 我已将 cmets 添加到第一种方法中。有关详细信息,请参阅the chdir manual page
【解决方案3】:

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

//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');

【讨论】:

  • 我的情况是第二种情况,这对我有用。
【解决方案4】:

做类似require(dirname(__FILE__) . '[RELATIVE PATH HERE]');的事情

【讨论】:

  • 你的意思是这样的: [code]include_once(dirname(FILE) . "../StoredProcedure/connect.php"); include_once(dirname(FILE) ."../untitled/sanitize_string.php"); ..它不起作用[/code]
  • dirname(FILE) 将返回包含包含代码的文件所在的当前目录,只需将相对路径附加到该文件即可。为方便起见,您可以打印 dirname(FILE) 的值并查看发生了什么。
  • 但它不能很好地工作 include_once(dirname(FILE)."../StoredProcedure/User/login.php");当我包含登录时,它会出现问题...您的代码行不起作用
  • 查看“daiscog”在问题下的评论并编写有问题的目录结构。
  • 已更新。 DIR 是否应该给我我所在的目录.. 所以如果我在我的网站的“无标题”目录中并且 home.php 在主目录中.. 如果我把相对路径前的目录名?
【解决方案5】:

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

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

文档在这里,可以比我更好地解释它:http://php.net/manual/en/language.oop5.autoload.php

真的会推荐使用它们,可以节省您的时间并且是更好的做法。

【讨论】:

  • 我知道它们..当您启动页面时,文件已加载..您将包含内容放入其中..但这并不能解决我的问题吗?
猜你喜欢
  • 2012-02-23
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 2013-11-30
  • 2012-12-11
  • 2011-06-08
  • 2011-11-15
相关资源
最近更新 更多