Php go back to root directory
Consider this project structure: Show
/path/to/projectroot/index.php header.php include/inc.php If index.php had
and inc.php had
You'd get that error since the line in inc.php would be looking for /path/to/projectroot/include/header.php (doesn't exist) not /path/to/projectroot/header.php (does exist) There are a few ways people resolve this. 1: Absolute paths The first, and most straightforward is to use absolute paths. If index.php had
and inc.php had
This would work. 2: Absolute paths with defines Similar to #1, if index.php had
and inc.php had
This would work. Update: As noted in the comments by pichan, you could use one of the "magic" constants here in index.php, so: index.php
and inc.php
Note we
add a trailing slash to
3: Include both and hide errors If inc.php had
This would work.[1] 4: Assume current directory unless otherwise specified If index.php had
and inc.php had
This would work. My take on these methods 1 and 2 are basically the same, but 2 is a little bit easier and more common for big projects since it allows you to make one constant definition and use it site-wide. It also allows you to deploy the project on multiple servers (placed in multiple paths) and only requires changing one line project-wide, as opposed to one line in each file for option 1. 3 is terrible, don't do it. Sometimes you'll see it, you might even see it in tutorials online. Don't do it. 4 should probably be avoided in favor of 1 or 2. But this approach might be necessary if you have some complex set of includes. Some Notes: [1] This is a terrible idea. It works, but don't do it. Posting back, maybe this will help someone else. It seems that part of my confusion is that there isn't one solution for specifying the root path for both HTML paths and PHP paths. At least I can't make one work. For HTML: I must have forgotten that gem. For PHP: However, in my particular instance it does so only on my local server. I am running my site on the Mosso Hosting cloud and $_SERVER['DOCUMENT_ROOT'] does not produce the correct server path, so I have to specify it manually. $_SERVER['DOCUMENT_ROOT'] does not work for me if echo'd as part of the image tag HREF path. C. Created: October-06, 2021 | Updated: March-21, 2022 We will introduce different methods to find the path of the root directory of a PHP project. Use the __DIR__ Predefined Constant to Find the Path of the Directory of a File in PHPIn PHP, there are predefined constants that can be used to achieve various functionalities. Suppose we
have a
Suppose we are currently working on Example Code:
Output:
Use the dirname() Function to Find the Path of the Root Directory of a Project in PHPThe function For example, we can consider the file structure as the first method. Working from the file, Example Code:
Output:
Use $_SERVER['DOCUMENT_ROOT'] to Find the Document Root Directory of a File in PHPWe can use the As shown in the output below, we found out the path Example Code:
Output:
Related Article - PHP FileHow do I get to the root directory?To locate the system root directory:
Press and hold the Windows key, then press the letter 'R'. (On Windows 7, you can also click start->run… to get the same dialog box.) Enter the word “cmd” in the program prompt, as shown, and press OK.
Where is the root directory in PHP?In order to get the root directory path, you can use _DIR_ or dirname(). echo dirname(__FILE__); Both the above syntaxes will return the same result.
What does __ DIR __ return?When you reference the __DIR__ inside a file, it returns the directory of the file. The __DIR__ doesn't include a trailing slash e.g., / or \ except it's a root directory. When you use the __DIR__ inside an include, the __DIR__ returns the directory of the included file.
What is __ DIR __ in PHP?The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
|