如何为 PHP 包含文件设置根文件夹

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

How to set root folder for PHP include files

phpincludeconfigworking-directory

提问by Vetaxili

I've spent days researching the internet and I can't find the answer that I will understand and are able to implement.

我花了几天时间研究互联网,但找不到我能理解并能够实施的答案。

I've got the website in which I want to use include_oncefiles and be able to view the website properly in both localhost and the actual server.

我有我想在其中使用include_once文件的网站,并且能够在本地主机和实际服务器中正确查看该网站。

I've used $_SERVER['DOCUMENT_ROOT']before but it doesn't work on the localhost so it is hard for me to update and do the changes to the website.

我以前用过$_SERVER['DOCUMENT_ROOT'],但它在本地主机上不起作用,所以我很难更新和对网站进行更改。

What I want to do is to create some sort of config.php file in which I can tell it that the root folder is either called htdocsor public_htmland to "start" looking for files from there.

我想要做的是建立某种形式的config.php文件的,我可以告诉它的根文件夹是名为htdocspublic_html和“开始”从寻找文件存在。

Or is there any other way I can get include paths to work?

或者有没有其他方法可以获得包含工作路径?

I've seen replies with e.g. dirname(__FILE__), __DIR__, etc. but they never explained how it would work, where to write it in the code, etc. I'm a bit sick and tired of finding part of the answer and then running around in circles just try and "fill in" the gaps. I'm not a PHP expert, just trying to make my life a bit easier by using it.

我看到过 eg dirname(__FILE__),__DIR__等的回复,但他们从来没有解释过它是如何工作的,在代码中写到哪里等等。我有点厌倦了找到部分答案然后绕圈子跑来跑去只需尝试“填补”空白。我不是 PHP 专家,只是想通过使用它让我的生活更轻松一些。

回答by K-Gun

dirname(__FILE__)and __DIR__both same and __DIR__comes with PHP 5.3

dirname(__FILE__)__DIR__两个相同的,__DIR__带有PHP 5.3

These are used in order to indicate that the "path of the file where they called".

这些用于指示“它们调用的文件的路径”。

URL: http://localhost/test/a.php

DIR: --NIX
     /var/www/test/a.php
     --WIN
     D:\lamp\www\test\a.php

// a.php's inside
<?php
echo __DIR__;

Gives you this on linux: /var/www/test

在 linux 上给你这个: /var/www/test

So, if you need a config parameter in all your project, just define it in your config.php and use it where you want both the file name that will be included.

因此,如果您在所有项目中都需要一个 config 参数,只需在您的 config.php 中定义它并在您希望包含文件名的地方使用它。

./
  config.php
  index.php
  header.php
  footer.php
  /lib
    foo.php
  /tmp
    bar.php

./config.php define('ROOT', __DIR__ .'/');

./config.php define('ROOT', __DIR__ .'/');

./index.php include_once(ROOT .'header.php'); ... include_once(ROOT .'footer.php');

./index.php include_once(ROOT .'header.php'); ... include_once(ROOT .'footer.php');

i.e, using it in tmp dir

即,在 tmp dir

./tmp/bar.php include_once(ROOT .'lib/foo.php');

./tmp/bar.php include_once(ROOT .'lib/foo.php');

UPDATE

更新

// config.php
<?php
define("ROOT", __DIR__ ."/");

So, we use this for index.phpto include banner.phpand banner.phpis waiting in ./banners/banner.php;

所以,我们用它index.php来包含banner.phpbanner.php等待./banners/banner.php

// index.php and the very first line!
<?php
include_once("config.php");
?>
// some html stuff
// ...
<?php include_once(ROOT ."banners/banner.php"); ?>
// some more html stuff
// ...

So, you should include config.php first to where you need it.

因此,您应该首先将 config.php 包含在您需要的地方。

I think, this is basic as far as needed...

我认为,这是基本的需要...

UPDATE

更新

So your problem is not PHP includesystem, but question, anyway... :)

所以你的问题不是 PHPinclude系统,而是问题,反正... :)

If your image path is changing (so not fixed), you can do like this;

如果您的图像路径正在更改(因此不固定),您可以这样做;

// config.php
define("ROOT", __DIR__ ."/");
define("HTTP", ($_SERVER["SERVER_NAME"] == "localhost")
   ? "http://localhost/your_work_folder/"
   : "http://your_site_name.com/"
);

// banner.php
<img src="<?php print HTTP; ?>images/banner.gif">

回答by jpevarnek

I normally set up what you mentioned, a config.php in an include directory, and have the following line in it: define('BASE_PATH', str_replace('/include', '', dirname(__FILE__)));("/include should be whatever directory your config.php file is in, if you have it in the root directory, you can just use define('BASE_PATH', dirname(__FILE__));). When I want to include any other file after that, I use include_once(BASE_PATH . '/directory/file.php');.

我通常会在包含目录中设置您提到的 config.php,并在其中包含以下行:(define('BASE_PATH', str_replace('/include', '', dirname(__FILE__)));“/include 应该是您的 config.php 文件所在的任何目录,如果您在根目录中有它,你可以只使用define('BASE_PATH', dirname(__FILE__));). 当我想在之后包含任何其他文件时,我使用include_once(BASE_PATH . '/directory/file.php');.

note: this concept is not original to me by any means.

注意:这个概念无论如何都不是我原创的。

回答by Whisperity

There is a function called getcwd()which will return the current working folder. If you call this at the very beginning of your script, you can store this, usually into a constant with define().

有一个函数被调用getcwd(),它将返回当前的工作文件夹。如果您在脚本的最开始调用它,您可以将它存储在一个常量中,通常使用define().

define('PROJECT_ROOT', getcwd());

回答by Pir Fahim Shah

To get/display the current work directory, php have a builtin function for that

要获取/显示当前工作目录,php 有一个内置函数

 echo getcwd() . "\n";

This will display the current directory of your php file which is now being executed.

这将显示正在执行的 php 文件的当前目录。

回答by JohnK

You can also change the include path, e.g.,

您还可以更改包含路径,例如,

$dir = dirname(__FILE__);
set_include_path($dir . '/../library' . PATH_SEPARATOR . $dir . '/../engine' . PATH_SEPARATOR . get_include_path());