php include_once,php中的相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7835948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:27:35  来源:igfitidea点击:

include_once, relative path in php

phpfileinclude-path

提问by Matrix001

I have 3 files: home, failed_attempt, login.

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

The file home and failed_attempt all refer to login file.

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

The annoying thing is that they throw a mistake saying that the login file doesnt exist. home will throw an exception if i do this, but failed_attempt wont.

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

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

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

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

and if I do this:

如果我这样做:

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

the opposite happens, failed_attempt throws an exception , but home, wont. How do I fix this..

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

Do I tell the include to go up a page by putting this ../ , and therefore home.php doesnt need to go one page up therefore it throws that exception..

我是否告诉包含通过将这个 ../ 上一页,因此 home.php 不需要上一页,因此它会抛出该异常..

How can I make it so both files accept those inclueds as valid.. perhaps not relative to where they are placed.. i.e. without that ../

我怎样才能做到这两个文件都接受那些有效的..也许不是相对于它们放置的位置..即没有那个../

Directory structure:

目录结构:

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

采纳答案by daiscog

Here are three possible solutions. The second are really just work-arounds that use absolute paths in a clever way.

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

1: chdir into the correct directory

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";
// ...
?>

Note that this will only work if your StoredProcedure folder is in the topmost directory of any files that might need to include the files it contains.

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

2: Use absolute paths

2:使用绝对路径

Now before you say this is not portable, it actually depends on how you implement it. Here's an example that works with Apache:

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

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

Alternatively, again with apache, put the following in your .htaccess in the root directory:

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

php_value auto_prepend_file /path/to/example.php

Then in example.php:

然后在example.php中:

<?php

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

?>

And finally in your files:

最后在你的文件中:

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

3: Set PHP's include_path

3:设置PHP的include_path

See the manual entry for the include_path directive. If you don't have access to php.ini, then this can be set in .htaccess, providing you are using Apache and PHP is notinstalled as CGI, like so:

请参阅include_path 指令的手册条目。如果您无权访问 php.ini,则可以在 .htaccess 中进行设置,前提是您使用的是 Apache 并且 PHP安装为 CGI,如下所示:

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

回答by CD001

Things like realpath()and __DIR__are your friends when it comes to creating paths in PHP.

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

Realpathhttp://php.net/manual/en/function.realpath.php

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

Magic constantshttp://php.net/manual/en/language.constants.predefined.php

魔法常量http://php.net/manual/en/language.constants.predefined.php

Since include and require are language constructs (rather than functions) they don't need brackets. Generally speaking you'd use includeto include "templates" (output files) and requireto include PHP files such as classes.

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

So you could use something like:

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

$sPath = realpath( __DIR__ . '/../relative/path/here');
if($sPath) { require_once $sPath; }

(use dirname(__FILE__)instead of __DIR__on PHP < 5)

(在 PHP < 5 上使用dirname(__FILE__)而不是__DIR__

It's worth evaluating the path before attempting to require the file as realpath()returns false if the file doesn't exist and attempting to require false;spaffs out a PHP error.

realpath()如果文件不存在并试图require false;发出 PHP 错误,则在尝试要求文件之前评估路径是值得的。

Alternatively you can just use absolute paths like:

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

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

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

回答by Reza Mamun

For the beginners this will help to understand. Use simply the following based on your relative path:

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

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

OR

或者

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

OR

或者

//include file from own child directory:
include_once('StoredProcedure/connect.php');

OR

或者

//include sibling files from same directory:
include_once('connect.php');

回答by Riz

Do something like require(dirname(__FILE__) . '[RELATIVE PATH HERE]');

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

回答by Riz

The other guys have answered your question correctly, however I thought I'd contribute something else: autoloaders.

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

Autoloaders allow you to define a function that will automatically include certain files, when you attempt to use a class.

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

The documentation is here and can explain it better than I can: http://php.net/manual/en/language.oop5.autoload.php

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

Would really recommend using them, will save you time and is better practice.

真的会推荐使用它们,将节省您的时间并且是更好的实践。