How to include PHP files located in different locations in different folders
P粉447002127
2023-08-22 00:21:10
<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>
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:
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:
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.
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
The included files under site 1 should be at:
Site 2
Actual code to access the include file in Site 1 from Site 2 :
If you try to access a file by excluding the
document root
androot
slashes, only the relative path of the file where the query is performed will be used (not absolute Reliable or non-platform specific):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:
Instead use:
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:Other possibilities:
Find your IP address and include the following code snippet:
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: