Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get webroot in PHP

Tags:

php

I am using Apache server for PHP. How can I retrieve my web root in PHP, like http://localhost/testthesis/?

like image 910
Aadi Avatar asked Mar 11 '10 11:03

Aadi


2 Answers

Relative paths

For your webservers root directory, use:

$folder = '/';

For the directory of the retrieved script, use:

$folder = './';

Absolute paths (from the client's perspective)

For your webservers root directory, use:

$protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://';
$folder = $protocol . $_SERVER['HTTP_HOST'];

For the directory of the retrieved script, use:

$protocol = $_SERVER['HTTPS'] == '' ? 'http://' : 'https://';
$folder = $protocol . $_SERVER['HTTP_HOST'] . '/' . basename($_SERVER['REQUEST_URI']);
like image 74
Phil Rykoff Avatar answered Oct 01 '22 19:10

Phil Rykoff


Here is one way of doing it:

$web_root = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/";

OUTPUT -->http://website.com/parent_folder/
like image 31
abbotto Avatar answered Oct 01 '22 20:10

abbotto