Get general project directory, php

2

How can I get the root of the project folder with PHP?

My searches have me use DIR however this returns the direction depending on the location of the file, and I want the project root, much called www, htdocs etc ...

Assuming this organization

www /   other folder /     other folder /       Index.php

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

    
asked by anonymous 11.11.2015 / 01:36

2 answers

2

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

In the console / terminal command, of course it will not return the path, but if you run from a browser you can see this path, which is a path defined in the configuration file server.

In a recent linux system, 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 will only be 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 add this file to 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 look 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're obviously going to get different paths and   different tabs too, but that's the idea.

    
11.11.2015 / 05:33
-1

You can do this as follows:

<?php

$raiz = $_SERVER['DOCUMENT_ROOT'];

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

Where pre-defined variables $ _SERVER ['DOCUMENT_ROOT'] returns the root directory over where the current script is running, for example you can see this by giving a var_dump in that variable.

var_dump($_SERVER['DOCUMENT_ROOT']);

In my environment the result is:

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

At the terminal you can get the current directory but not the root by the following function:

<?php

echo getcwd();

Resulted 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, by calling dirname within dirname to the root of your project:

<?php

var_dump(dirname(__FILE__));

var_dump(dirname(dirname(__FILE__)));

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

Result:

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"
    
11.11.2015 / 02:58