PHP 需要顶级目录中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7630002/
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:04:00  来源:igfitidea点击:

PHP require file from top directory

phpincludedirectoryrequire

提问by Lucas Penney

I have several subdomains contained in their own directory above my root site, and an assets folder in my root directory. For example:

我有几个子域包含在我的根站点上方的它们自己的目录中,并且在我的根目录中有一个 assets 文件夹。例如:

/
/assets/
/forums/
/blog/

I'm trying to require() files on php pages in both the root directory, and the subdirectories, for example, assets/include/form.php is required() in both index.php in the root directory and index.php in the forums directory. However, inside form.php, is another require, trying to get another file contained in the include directory.

我试图在根目录和子目录中的php页面上都需要()文件,例如,在根目录中的index.php和index.php中都需要assets/include/form.php()论坛目录。然而,在 form.php 中,还有一个 require,试图获取包含在 include 目录中的另一个文件。

I can't seem to find a way to require a file inside this form.php where it will work on both the root directory's index.php and the forum directory's index.php.

我似乎无法找到一种方法在这个 form.php 中需要一个文件,它可以在根目录的 index.php 和论坛目录的 index.php 上工作。

Is there any way I can explicitly require a file starting from the root directory?

有什么方法可以明确要求从根目录开始的文件?

回答by CheeseSucker

There are several ways to achieve this.

有几种方法可以实现这一点。

Use relative path from form.php

使用相对路径 form.php

require_once __DIR__ . '/otherfile.php';

If you're using PHP 5.2 or older, you can use dirname(__FILE__)instead of __DIR__. Read more about magic constants here.

如果你使用PHP 5.2或以上,则可以使用dirname(__FILE__)替代__DIR__在此处阅读有关魔术常量的更多信息。

Use the $_SERVER['DOCUMENT_ROOT']variable

使用$_SERVER['DOCUMENT_ROOT']变量

This is the absolute path to your document root: /var/www/example.org/or C:\wamp\www\on Windows.

这是文档根目录的绝对路径:/var/www/example.org/C:\wamp\www\在 Windows 上。

The document root is the folder where your root level files are: http://example.org/index.phpwould be in $_SERVER['DOCUMENT_ROOT'] . '/index.php'

文档根目录是您的根级别文件所在的文件夹:http: //example.org/index.php将在$_SERVER['DOCUMENT_ROOT'] . '/index.php'

Usage: require_once $_SERVER['DOCUMENT_ROOT'] . '/include/otherfile.php';

用法: require_once $_SERVER['DOCUMENT_ROOT'] . '/include/otherfile.php';

Use an autoloader

使用自动装载机

This will probably be a bit overkill for your application if it's very simple.

如果它非常简单,这对您的应用程序来说可能有点矫枉过正。

Set up PHP's include paths

设置 PHP 的包含路径

You can also set the directories where PHP will look for the files when you use require()or include(). Check out set_include_path()here.

您还可以设置 PHP 在使用require()或时查找文件的目录include()。在set_include_path()这里查看

Usage: require_once 'otherfile.php';

用法: require_once 'otherfile.php';



Note:

笔记:

I see some answers suggest using the URL inside a require(). I would not suggest this as the PHP file would be parsed before it's included. This is okay for HTML files or PHP scripts which output HTML, but if you only have a class definition there, you would get a blank result.

我看到一些答案建议使用require(). 我不建议这样做,因为 PHP 文件会在被包含之前被解析。这对于输出 HTML 的 HTML 文件或 PHP 脚本来说是可以的,但是如果您在那里只有一个类定义,您将得到一个空白结果。

回答by JWC May

If one level higher use:

如果上一级使用:

include('../header.php');

if two levels higher:

如果高两个级别:

include('../../header.php');

Fore more, visit: http://forums.htmlhelp.com/index.php?showtopic=2922

更多信息,请访问:http: //forums.htmlhelp.com/index.php?showtopic=2922

回答by Ecko

Maybe this isn't correct topic and maybe not correct use, but maybe it will help someone. When I wanted to make links in menu from absolute path, as I was jumping from directory to directory and paths were nonstop changing, I wanted to fix to base dir.

也许这不是正确的主题,也许不是正确的使用,但也许它会帮助某人。当我想从绝对路径在菜单中建立链接时,因为我从一个目录跳转到另一个目录并且路径不停地改变,我想修复到基本目录。

So instead of using '../' to go sub-folder (as I didn't know many I needed to go down ../../../), I wrote link as <a href='/first_dir/second_dir/third_dir//..'>, similar it works also with <img src='/first_dir/second_dir//../logo.svg'>. Unfortunately this doesn't work with require_once(). PHP 7

因此,我没有使用 '../' 进入子文件夹(因为我不知道很多我需要向下 ../../../),我将链接写为<a href='/first_dir/second_dir/third_dir//..'>,类似它也适用于<img src='/first_dir/second_dir//../logo.svg'>. 不幸的是,这不适用于require_once(). PHP 7

回答by xXPhenom22Xx

You have 2 solutions. You can either use the full URL to the file, for instance if your file is in the 'lib' directory you can use: require_once ('http://www.example.com/lib/myfile.php')

您有 2 个解决方案。您可以使用文件的完整 URL,例如,如果您的文件位于“lib”目录中,您可以使用:require_once (' http://www.example.com/lib/myfile.php')

or

或者

You can try the $_SERVER['DOCUMENT_ROOT']function in PHP but this isnt always fool proof.

您可以$_SERVER['DOCUMENT_ROOT']在 PHP 中尝试该功能,但这并不总是万无一失。

回答by jorge soares

  • suppose you have pgm1.php in www.domain.com/dir1/dir2/dir3/pgm1.php, which include or require 'dirA/inc/Xpto.php'.
  • and another pgm2.php in www.domain.com/dirZ1/dirZ2/pgm2.php, which include or require same 'dirA/inc/Xpto.php'.
  • and your Host configuration not authorize require/include 'http://www.domain.com/......'
  • So...you can use this, in both pgm1.php & pgm1.php, and REQUIRE or
    INCLUDE works !

    function rURI($p){
       for($w='',$i=1;$i<(count(explode('/',$_SERVER['PHP_SELF']))-1);$i++,$w.='../');return
       $w.$p; }
    

    require rURI('dirA/inc/Xpto.php'); // path from root

  • 假设您在 www.domain.com/dir1/dir2/dir3/pgm1.php 中有 pgm1.php,其中包含或需要“dirA/inc/Xpto.php”。
  • 和 www.domain.com/dirZ1/dirZ2/pgm2.php 中的另一个 pgm2.php,其中包含或需要相同的“dirA/inc/Xpto.php”。
  • 并且您的主机配置未授权 require/include ' http://www.domain.com/......'
  • 所以......你可以在 pgm1.php 和 pgm1.php 中使用它,并且 REQUIRE 或
    INCLUDE 工作!

    function rURI($p){
       for($w='',$i=1;$i<(count(explode('/',$_SERVER['PHP_SELF']))-1);$i++,$w.='../');return
       $w.$p; }
    

    需要 rURI('dirA/inc/Xpto.php'); // 从根开始的路径

回答by Ahmad Yousef

why don't you use (../)

你为什么不使用 (../)

require('../folder/file.php');