PHP absolute path to root
Create a constant with absolute path to the root by using define
in ShowInfo.php
:
define('ROOTPATH', __DIR__);
Or PHP <= 5.3
define('ROOTPATH', dirname(__FILE__));
Now use it:
if (file_exists(ROOTPATH.'/Texts/MyInfo.txt')) {
// ...
}
Or use the DOCUMENT_ROOT
defined in $_SERVER
:
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/Texts/MyInfo.txt')) {
// ...
}
PHP absolute root path and root url
Absolute root path:
$_SERVER['DOCUMENT_ROOT']
Path from the root folder:
dirname($_SERVER['PHP_SELF'])
Get Root Directory Path of a PHP project
For PHP >= 5.3.0 try
PHP magic constants.
__DIR__
And make your path relative.
For PHP < 5.3.0 try
dirname(__FILE__)
Way to get absolute path of file PHP
You can try like this
include dirname(__FILE__).'/../yourfile.php';
Since PHP 5.3.0 you can use also the magic constant __DIR__
More info in the docs
Another way is
$rootDir = realpath($_SERVER["DOCUMENT_ROOT"]);
include "$rootDir/yourfile.php";
Define root path one time at one place
You can use auto_prepend_file
in your .htaccess
PS: $_SERVER['DOCUMENT_ROOT']
should work (on shared hostings too)
How can I build a dynamic absolute path in PHP?
By using the built-in constant __DIR__
you can get your absolute path
by print it or save
echo __DIR__;
it will print something like this for you
C:\Users\user\Desktop\test
and there is also another constant that will get the absolute path
for your file that executes the command __FILE__
echo __FILE__;
will give you a result like this
C:\Users\user\Desktop\test\index.php
Path of current PHP script relative to document root
What I've done in the past is to use a setup like this:
define('DIR_BASE', dirname( __FILE__ ).'/');
define('DIR_SYSTEM', DIR_BASE.'app/');
etc.
This would be placed in a file root of your project, which will give you access to other areas of your file structure relative to the root.
For your case, it would look like this:
define('DIR_BASE', dirname( __FILE__ ).'/');
define('DIR_CSS', DIR_BASE.'css/');
define('DIR_PHP', DIR_BASE.'php/');
define('DIR_CONTENT' DIR_BASE.'content/');
This would go in getpath.php in your root, and would give you the ability to reference any file or directory regardless of where it is placed in the directory structure (be sure to include it wherever you'll be using them). You wouldn't need to change your directory structure or anything like that, and you don't have to worry about any vulnerabilities with something like this, since it's internal.
Edit
I keep coming back to the structure of the system. Is this a multi-site system that uses the same CSS through out? If not, then what is the justification for having the directory structure laid out like this? Can you give just a little more detail please?
What is the correct path to a file below the root?
The simple solution was to use the absolute path:
<?php
require '/home/bitnami/vendor/required.php';
Related Topics
How to Use Mamp's Version of PHP Instead of the Default on Osx
Stop Caching for PHP 5.5.3 in Mamp
Best Practices: Working with Long, Multiline Strings in PHP
File_Get_Contents Behind a Proxy
Split String by Delimiter, But Not If It Is Escaped
Back to Previous Page with Header( "Location: " ); in PHP
Send Email with PHPmailer - Embed Image in Body
Laravel Recursive Relationships
You Must Enable the Openssl Extension to Download Files via Https
How to Get Customer Details from an Order in Woocommerce
Passing Data to Buildform() in Symfony 2.8, 3.0 and Above
PHP Pdo VS Normal MySQL_Connect
Why Can't PHP Create a Directory with 777 Permissions
How to Convert Datetime to Iso 8601 in PHP
Handling Expired Token in Laravel
How to Rename Sub-Array Keys in PHP