<tfoot id='krmxX'></tfoot>

        <small id='krmxX'></small><noframes id='krmxX'>

      1. <legend id='krmxX'><style id='krmxX'><dir id='krmxX'><q id='krmxX'></q></dir></style></legend>
        • <bdo id='krmxX'></bdo><ul id='krmxX'></ul>
      2. <i id='krmxX'><tr id='krmxX'><dt id='krmxX'><q id='krmxX'><span id='krmxX'><b id='krmxX'><form id='krmxX'><ins id='krmxX'></ins><ul id='krmxX'></ul><sub id='krmxX'></sub></form><legend id='krmxX'></legend><bdo id='krmxX'><pre id='krmxX'><center id='krmxX'></center></pre></bdo></b><th id='krmxX'></th></span></q></dt></tr></i><div id='krmxX'><tfoot id='krmxX'></tfoot><dl id='krmxX'><fieldset id='krmxX'></fieldset></dl></div>

        PHP 需要顶级目录中的文件

        时间:2023-07-15

          <bdo id='196uQ'></bdo><ul id='196uQ'></ul>
            <tbody id='196uQ'></tbody>

            <small id='196uQ'></small><noframes id='196uQ'>

                <i id='196uQ'><tr id='196uQ'><dt id='196uQ'><q id='196uQ'><span id='196uQ'><b id='196uQ'><form id='196uQ'><ins id='196uQ'></ins><ul id='196uQ'></ul><sub id='196uQ'></sub></form><legend id='196uQ'></legend><bdo id='196uQ'><pre id='196uQ'><center id='196uQ'></center></pre></bdo></b><th id='196uQ'></th></span></q></dt></tr></i><div id='196uQ'><tfoot id='196uQ'></tfoot><dl id='196uQ'><fieldset id='196uQ'></fieldset></dl></div>
              • <tfoot id='196uQ'></tfoot>
              • <legend id='196uQ'><style id='196uQ'><dir id='196uQ'><q id='196uQ'></q></dir></style></legend>

                1. 本文介绍了PHP 需要顶级目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  <预><代码>//资产//论坛//博客/

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

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

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

                  解决方案

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

                  使用来自 form.php

                  的相对路径

                  require_once __DIR__ .'/otherfile.php';

                  如果您使用的是 PHP 5.2 或更低版本,则可以使用 dirname(__FILE__) 而不是 __DIR__.在此处阅读有关魔术常量的更多信息.

                  使用$_SERVER['DOCUMENT_ROOT']变量

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

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

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

                  使用自动加载器

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

                  设置 PHP 的包含路径

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

                  用法:require_once 'otherfile.php';

                  <小时>

                  注意:

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

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

                  /
                  /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.

                  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.

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

                  解决方案

                  There are several ways to achieve this.

                  Use relative path from 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.

                  Use the $_SERVER['DOCUMENT_ROOT'] variable

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

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

                  Usage: 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

                  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.

                  Usage: 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.

                  这篇关于PHP 需要顶级目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:压缩目录中的所有文件并下载生成的 .zip 下一篇:使用 php 删除所有文件、文件夹及其子文件夹

                  相关文章

                  <legend id='uksby'><style id='uksby'><dir id='uksby'><q id='uksby'></q></dir></style></legend>
                2. <i id='uksby'><tr id='uksby'><dt id='uksby'><q id='uksby'><span id='uksby'><b id='uksby'><form id='uksby'><ins id='uksby'></ins><ul id='uksby'></ul><sub id='uksby'></sub></form><legend id='uksby'></legend><bdo id='uksby'><pre id='uksby'><center id='uksby'></center></pre></bdo></b><th id='uksby'></th></span></q></dt></tr></i><div id='uksby'><tfoot id='uksby'></tfoot><dl id='uksby'><fieldset id='uksby'></fieldset></dl></div>

                        <bdo id='uksby'></bdo><ul id='uksby'></ul>

                      <small id='uksby'></small><noframes id='uksby'>

                      <tfoot id='uksby'></tfoot>