Why DOCUMENT_ROOT is different than realpath('

2019-05-21 02:03发布

问题:

with

echo realpath('.').'<br>';
echo dirname(__FILE__).'<br>';
echo realpath(dirname(__FILE__)).'<br>';
echo $_SERVER[PHP_SELF].'<br>'; 
echo getcwd();

I get always

/services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public

but in phpinfo DOCUMENT_ROOT is

/services/webpages/l/i/mydomain.com/public

because of that I'm having hard times with .htaccess in conjunction with Zend Framework.

On my local I'm able to make it work. But on the provider host I could not grasp the magic yet.

EDT: I put $_SERVER['DOCUMENT_ROOT'] = realpath($_SERVER['DOCUMENT_ROOT']); on the index file but got this.

Fatal error: Uncaught exception 'Zend_Config_Exception' with message 'parse_ini_file(/services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public/v0.2b/application/configs/appkeys.ini) [function.parse-ini-file]: failed to open stream: No such file or directory parse_ini_file(/services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public/v0.2b/application/configs/appkeys.ini) [function.parse-ini-file]: failed to open stream: No such file or directory' in /services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public/v0.2b/library/Zend/Config/Ini.php:181 Stack trace: #0 /services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public/v0.2b/library/Zend/Config/Ini.php(201): Zend_Config_Ini->_parseIniFile('/services2/webp...') #1 /services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public/v0.2b/library/Zend/Config/Ini.php(125): Zend_Config_ in /services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public/v0.2b/library/Zend/Config/Ini.php on line 181

EDT: thats the one:

$appkeys = new Zend_Config_Ini(APPLICATION_PATH . '/configs/appkeys.ini');

and APPLICATION_PATH is defined by

defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

回答1:

Try this:

$_SERVER['DOCUMENT_ROOT'] = realpath($_SERVER['DOCUMENT_ROOT']);
echo $_SERVER['DOCUMENT_ROOT'] . "\n";

And read this



回答2:

It could be that some of the directories in your DOCUMENT_ROOT are actually symlinks to other directories - to quote the documentation,

realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and return the canonicalized absolute pathname.

Therefore, if (for example), /services/webpages/l is a directory, and /services/webpages/l/i is a symlink pointing to /services2/webpages/util/i/g/gg8375620.provider.com.br, both /services2/webpages/util/i/g/gg8375620.provider.com.br/mydomain.com/public and /services/webpages/l/i/mydomain.com/public will get you to the same directory, but realpath() will always return the former.

/services
 + webpages
 | - util
 |   - i
 |     - g 
 |       - gg8375620.provider.com.br
 |         - mydomain.com
 |           - public
 + l
   - i -> ../webpages/util/i/g/gg8375620.provider.com.br/


回答3:

document_root is coming from your web server configuration file that is why is it different from realpath function.



回答4:

Well, DOCUMENT_ROOT environment variable is just set to wrong value.
If you have no control of this server, you can't fix it yourself.
Either ask support to fix or find a way to avoid this variable use

As it seems as ZF problem, I think it's easy to solve.
I never used that framework but I am sure it has single entry point or some config file.
So, you can easily fake DOCUMENT ROOT by setting something like this:

$_SERVER['DOCUMENT_ROOT'] = realpath(dirname(__FILE__).'/../'); 

where '/../' represents relative path from such an entry point to the actual document root



回答5:

echo realpath(dirname(FILE));

this should be helpful. on this link http://www.tech99.us/finding-absolute-path-in-php/you can find details for using these functions. each function used in above line works with php4 and php5.