How to include PHP files located in different locations in different folders
P粉447002127
P粉447002127 2023-08-22 00:21:10
0
2
557
<p>Most of my site is in the root directory. In this directory there are "css", "functions" and "images" folders. When I include the php file in index.php or other root files everything works fine. It contains and executes correctly. </p> <p>But the problem arises when I create a folder called "blog". This is a brand new, separate root folder with the CMS and its own "root" file. When I try to include css from the main root or a php file from the "functions" folder in the main root, everything crashes. I know I have to include it using <code>../functions/myfile.com</code>. But this file also contains other files, so it doesn't work and doesn't include the other files correctly. </p> <p>Is there any way to solve this problem? </p>
P粉447002127
P粉447002127

reply all(2)
P粉976737101

If I understand you correctly, you have two folders, one is where the php script is that you want to include into the other folder?

If this is the case, you just need to follow the path correctly. Assume your folder structure is as follows:

root
    includes
        php_scripts
            script.php
    blog
        content
            index.php

If this is the folder structure you came up with, and you want to include the "Script.php" file into the "index.php" folder, you need to include it like this:

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

The way I do it is visually. I put my mouse pointer over index.php (to see the file structure), and every time I go up a folder, I type another "../". Then you have to make sure that you go up into the folder structure above the folder you want to start going into. After that, it's the normal folder hierarchy.

P粉614840363

You can access the root directory from within each site using $_SERVER['DOCUMENT_ROOT']. Just for testing, you can print out the path to make sure it's working, if you're doing it the right way. You definitely don't want to display the local server path, e.g. contains and requires .

Site 1

echo $_SERVER['DOCUMENT_ROOT']; //应该是 '/main_web_folder/';

The included files under site 1 should be at:

echo $_SERVER['DOCUMENT_ROOT'].'/includes/'; // 应该是 '/main_web_folder/includes/';

Site 2

echo $_SERVER['DOCUMENT_ROOT']; //应该是 '/main_web_folder/blog/';

Actual code to access the include file in Site 1 from Site 2 :

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

If you try to access a file by excluding the document root and root slashes, only the relative path of the file where the query is performed will be used (not absolute Reliable or non-platform specific):

include('../includes/file_from_site_1.php');

The included path has no place in any code on the site's front-end (online) and should be used safely in production environments only .

Additionally, for the URLs of the site itself, you can make them relative to the domain name. Browsers automatically fill in the rest because they know what page they are looking at. So, don't use:

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

Instead use:

<a href='/contact/'>联系</a>

For good SEO, you need to ensure that your blog's URL does not exist on another domain, otherwise it may be marked as a duplicate site. In this case, you may also want to add a line to the robots.txt file that is limited to site 1:

User-agent: *
Disallow: /blog/

Other possibilities:

Find your IP address and include the following code snippet:

function is_dev(){
  //使用来自Google的外部IP。
  //如果您在本地托管,它是127.0.01,除非您更改了它。
  $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, for example if you have a DCHP dynamic IP, you will need to change the IP in this file to see the results. I recommend putting that file in an include file and then requiring it on the debug page.

If you have access to modern methods such as the browser's console log , you can use the following code and view it in the browser's debug interface:

if(is_dev()){
    echo "<script>".PHP_EOL;
    echo "console.log('".$_SERVER['DOCUMENT_ROOT']."');".PHP_EOL;
    echo "</script>".PHP_EOL;       
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template