包含在不同文件夹中的 php 文件

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

Include php files when they are in different folders

phpinclude

提问by FosAvance

Most of my website is in my root directory. And In that directory there is "css", "functions", "images" folder. Everything works fine when I include php files within index.php or any other root file. It includes it fine and executes it fine.

我的大部分网站都在我的根目录中。在该目录中有“css”、“functions”、“images”文件夹。当我在 index.php 或任何其他根文件中包含 php 文件时,一切正常。它包含它并很好地执行它。

But problem occurres when I made folder "blog". So this is totally new and separate root folder with CMS and its own "root" files. And I try to include css from main root directory or some php files from "functions" folder in main root directory, Everything breaks down. I know I have to include it as ../functions/myfile.com. But this files includes some other files so it just wont work properly and won't be able to include other files properly.

但是当我制作文件夹“博客”时出现问题。所以这是一个全新的、独立的根文件夹,带有 CMS 和它自己的“根”文件。我尝试包含主根目录中的 css 或主根目录中“functions”文件夹中的一些 php 文件,一切都崩溃了。我知道我必须将其包含为../functions/myfile.com. 但是此文件包含一些其他文件,因此它无法正常工作并且无法正确包含其他文件。

Is there any idea how to fix this problem?

有什么想法如何解决这个问题吗?

回答by Absolute?ER?

You can get to the rootfrom within each site using $_SERVER['DOCUMENT_ROOT']. For testing ONLY you can echo out the path to make sure it's working, if you do it the right way.You NEVER want to show the local server paths for things like includesand requires.

你可以获取到使用每个站点内$_SERVER['DOCUMENT_ROOT']仅用于测试,如果您以正确的方式进行,您可以回显路径以确保其正常工作您永远不想显示包含需要内容的本地服务器路径

Site 1

站点 1

echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/';

Includes under site one would be at:

包括在站点一下:

echo $_SERVER['DOCUMENT_ROOT'].'/includes/'; // should be '/main_web_folder/includes/';

Site 2

站点 2

echo $_SERVER['DOCUMENT_ROOT']; //should be '/main_web_folder/blog/';

The actual code to access includes from site1inside of site2you would say:

要访问的实际代码包括来自site2内的site1,您会说:

include($_SERVER['DOCUMENT_ROOT'].'/../includes/file_from_site_1.php');

It will only use the relative pathof the file executing the query if you try to access it by excluding the document rootand the rootslash:

如果您尝试通过排除和斜杠来访问它,它只会使用执行查询的文件的相对路径document rootroot

 //(not as fool-proof or non-platform specific)
 include('../includes/file_from_site_1.php');

Included paths have no place in code on the front end (live) of the site anywhere, and should be secured and used in production environments only.

包含的路径在站点前端(实时)任何地方的代码中都没有位置,并且应该生产环境中进行保护和使用。

Additionally for URLs on the site itself you can make them relative to the domain. Browsers will automatically fill in the rest because they know which page they are looking at. So instead of:

此外,对于站点本身的 URL,您可以使它们相对于域。浏览器会自动填写其余部分,因为它们知道他们正在查看哪个页面。所以而不是:

<a href='http://www.__domain__name__here__.com/contact/'>Contact</a>

You should use:

你应该使用:

<a href='/contact/'>Contact</a>

For good SEO you'll want to make sure that the URLs for the blog do not exist in the other domain, otherwise it may be marked as a duplicate site. With that being said you might also want to add a line to your robots.txtfile for ONLYsite1:

为了获得良好的 SEO,您需要确保该博客的 URL 不存在于其他域中,否则它可能会被标记为重复站点。话虽如此,您可能还想在robots.txt文件中为ONLY site1添加一行:

User-agent: *
Disallow: /blog/

Other possibilities:

其他可能性:

Look up your IP addressand include this snippet of code:

查找您的 IP 地址并包含以下代码片段:

function is_dev(){
  //use the external IP from Google.
  //If you're hosting locally it's 127.0.01 unless you've changed it.
  $ip_address='xxx.xxx.xxx.xxx';

  if ($_SERVER['REMOTE_ADDR']==$ip_address){
     return true;
  } else {
     return false;
  } 
}

if(is_dev()){
    echo $_SERVER['DOCUMENT_ROOT'];       
}

Remember if your ISP changes your IP, as in you have a DCHP Dynamic IP, you'll need to change the IP in that file to see the results. I would put thatfile in an include, then require it on pages for debugging.

请记住,如果您的 ISP 更改了您的IP,因为您有一个 DCHP 动态 IP,您将需要更改该文件中的 IP 以查看结果。我会把那个文件放在一个包含中,然后在页面上要求它进行调试。

If you're okay with modern methods like using the browser console logyou could do this instead and view it in the browser's debugging interface:

如果您对使用浏览器控制台日志等现代方法没问题,您可以改为执行此操作并在浏览器的调试界面中查看它:

if(is_dev()){
    echo "<script>".PHP_EOL;
    echo "console.log('".$_SERVER['DOCUMENT_ROOT']."');".PHP_EOL;
    echo "</script>".PHP_EOL;       
}

回答by ntgCleaner

If I understand you correctly, You have two folders, one houses your php script that you want to includeinto a file that is in another folder?

如果我理解正确,您有两个文件夹,其中一个将您想要include放入另一个文件夹中的文件的php 脚本保存?

If this is the case, you just have to follow the trail the right way. Let's assume your folders are set up like this:

如果是这种情况,您只需要按照正确的方式走即可。假设您的文件夹设置如下:

root
    includes
        php_scripts
            script.php
    blog
        content
            index.php

If this is the proposed folder structure, and you are trying to include the "Script.php" file into your "index.php" folder, you need to include it this way:

如果这是建议的文件夹结构,并且您尝试将“Script.php”文件包含到“index.php”文件夹中,则需要以这种方式包含它:

include("../../../includes/php_scripts/script.php");

The way I do it is visual. I put my mouse pointer on the index.php (looking at the file structure), then every time I go UP a folder, I type another "../" Then you have to make sure you go UP the folder structure ABOVE the folders that you want to start going DOWN into. After that, it's just normal folder hierarchy.

我这样做的方式是视觉上的。我把我的鼠标指针放在 index.php 上(查看文件结构),然后每次我上一个文件夹时,我输入另一个“../”然后你必须确保你上文件夹结构上面的文件夹你想开始往下走。之后,它只是普通的文件夹层次结构。

回答by MBouwman

None of the above answers fixed this issue for me. I did it as following (Laravel with Ubuntu server):

以上答案都没有为我解决这个问题。我是这样做的(Laravel 和 Ubuntu 服务器):

<?php
     $footerFile = '/var/www/website/main/resources/views/emails/elements/emailfooter.blade.php';
     include($footerFile);
?>

回答by Niels Keurentjes

Try to never use relative paths. Use a generic include where you assign the DocumentRoot server variable to a global variable, and construct absolute paths from there. Alternatively, for larger projects, consider implementing a PSR-0 SPL autoloader.

尽量不要使用相对路径。在将 DocumentRoot 服务器变量分配给全局变量的位置使用通用包含,并从那里构造绝对路径。或者,对于较大的项目,请考虑实施 PSR-0 SPL 自动加载器。