Get general project directory, php

Asked

Viewed 12,596 times

3

How to get the root of the project folder with PHP?

My research tells me to use DIR however this returns the direction depending on the location of the file, and I want the root of the project, very called www, htdocs etc...

Assuming such an organization

www/ otherness/ otherness/ Index.php

Regardless of where the file is located, I always want to get the www directory..

  • 1

    Utilizes the $_SERVER['DOCUMEN_ROOT'], and creates a constant DIR based on that address.

  • 2

    If you will use in the terminal $_SERVER['DOCUMEN_ROOT'] will come empty. However as you are saying that you want the root of the document, then it is understood that you want to use via web server. So I suggest that you create a constant that points to the root directory at the beginning of the code, or in your application’s configuration file. So in any subsequent file you will have this constant available. The constant will work for both command line calls and web server calls.

2 answers

3

As already explained, it uses variable $_SERVER, you should not expect all browsers/systems to return the same thing, because some of these sometimes omit information, but what I say is, use this variable only to find out which is the directory root you are currently using or will use, directly using that variable or based only on the path it returns.

In the console/terminal of commands, of course it will not return the path, but if you run from a browser you will be able to view this path, which by chance is a path defined in the server configuration file.

In a system linux recent, you will get /var/www/meu_site, using $_SERVER['DOCUMENT_ROOT'].

Based on this, you can simply define all possible paths for your application in a single configuration file, which is(ia) called once.

// init.php
// Ficheiro de inicialização de configurações gerais
define('DS', DIRECTORY_SEPARATOR);
define('ROOT',$_SERVER['DOCUMENT_ROOT']);
define('SITE_ROOT',ROOT.DS.'meu_site');
# 2º alternativa
# define('SITE_ROOT',DS.'var'.DS.'www'.DS.'meu_site');
define('LIB_CLASS',SITE_ROOT.DS.'classes');
define('LIB_INCLUDES',SITE_ROOT.DS.'includes');

So just include this file in the script:

require_once '/includes/init.php';
// Noutros ficheiros que possuam a configuração de inicialização
print "<h3>Diretórios:</h3>";
print SITE_ROOT.DS.'teste.php';
//require_once SITE_ROOT.DS.'teste.php';
print "<br/>";
print LIB_CLASS.DS.'class.teste.php';
//require_once LIB_CLASS.DS.'class.teste.php';
print "<br/>";
print LIB_INCLUDES.DS.'testeValidar.php';
//require_once LIB_INCLUDES.DS.'testeValidar.php';

The paths will be like this:

/var/www/meu_site/teste.php
/var/www/meu_site/classes/class.teste.php
/var/www/meu_site/includes/testeValidar.php

For windows, you will obviously get different paths and different separators as well, but this is the idea.

-1

You can do it this way:

<?php

$raiz = $_SERVER['DOCUMENT_ROOT'];

require_once $raiz . '/outrapasta/file_require_1.php';
require_once $raiz . '/outrapasta/file_require_2.php';

Where the predefined variables $_SERVER['DOCUMENT_ROOT'] returns the root directory over where the current script is running, as for example you can observe this by giving a var_dump on this variable.

var_dump($_SERVER['DOCUMENT_ROOT']);

In my environment the result is:

string 'C:/xampp-1.8.2/htdocs/fypa/stackoverflow' (length=40)

By terminal you can grab the current directory, but not the root by the following function:

<?php

echo getcwd();

Lost in my environment:

C:\xampp-1.8.2\htdocs\fypa\stackoverflow

To include some file:

<?php

$raiz = getcwd();

require_once $raiz . '/outrapasta/file_require_1.php';

Or so too, calling dirname inside dirname to the root of your project:

<?php

var_dump(dirname(__FILE__));

var_dump(dirname(dirname(__FILE__)));

var_dump(dirname(dirname(dirname(__FILE__))));

Upshot:

string(40) "C:\xampp-1.8.2\htdocs\fypa\stackoverflow"
string(26) "C:\xampp-1.8.2\htdocs\fypa"
string(21) "C:\xampp-1.8.2\htdocs"

  • when running on the terminal the value of $_SERVER['DOCUMENT_ROOT'] is ""

  • When being run a script from a file in the terminal is not run the file but its contents, so turn empty the $_SERVER['DOCUMENT_ROOOT']where there would be no way for it to return the root folder and it is running as if it had been typed in the terminal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.