Sloth Armstrong
Sloth Armstrong

Reputation: 1066

$_SERVER['DOCUMENT_ROOT'] leads to unresolved paths in PHPStorm

I am currently writing a project in PHPStorm and am having issues with the editor resolving paths that are prefixed with the server's document root. An example line of this might be:

require_once($_SERVER["DOCUMENT_ROOT"] . "/file.php");

This evaluates on the server and the page is built properly. PHPStorm however is complaining and saying "Path 'file.php' not found". I have looked into this issue and found threads online such as this that outline this situation. I have tried setting the Resource Root as recommended but have had no luck. Any help or guidance as to why this is happening would be appreciated. Thanks!

Edit: The document root evaluates to the local drive on my laptop that I am using to debug on a local Apache instance - "/Users/Me/PhpstormProjects/MyProject". This path is also set as the Resource Root in PHPStorm.

Upvotes: 1

Views: 2502

Answers (4)

Romchik
Romchik

Reputation: 121

PHP Storm 2020

File -> Settings -> Languages & Frameworks -> PHP -> (Tab) Analysis

Field Include Analysis -> type your folder name here (e.g. www).

Upvotes: 2

LazyOne
LazyOne

Reputation: 165088

Evaluation of $_SERVER["DOCUMENT_ROOT"] is not supported and currently there is no way to set it up somehow.

http://youtrack.jetbrains.com/issue/WI-3321 -- watch/star it to get notified on progress.


If you are happy to change your code a bit .. you could use this approach (works best if you have single point entry into your app -- e.g. all requests are routed via index.php or alike):

  • define some constant there (e.g. define('DIR_ROOT', __DIR__); -- since index.php is usually placed in website root, it will point to the same folder as $_SERVER["DOCUMENT_ROOT"])
  • use it in any other file, i.e. require_once(DIR_ROOT . "/file.php");

If you are including classes this way -- maybe you should use autoloader instead (no need to do this manually)?


UPDATE ( 26/05/2015 ):

$_SERVER["DOCUMENT_ROOT"] is supported since PhpStorm v8.0.3 and it's resolved to the project root, so if your web server root is located in actual subfolder (e.g. /some/path/project_root/www) it will not get resolved correctly.

Upvotes: 7

Web User
Web User

Reputation: 330

The latest version of PHPStorm EAP fixes this bug.

Screenshot working $_SERVER["DOCUMENT_ROOT"]

You can get the last EAP here: https://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Early+Access+Program

Upvotes: 0

kalvatn
kalvatn

Reputation: 198

http://php.net/manual/en/ini.core.php#ini.include-path, add the document root to the include path (echo $_SERVER["DOCUMENT_ROOT"];)

can also do this at runtime using http://php.net/manual/en/function.set-include-path.php

might also want to see : http://www.jetbrains.com/phpstorm/webhelp/configuring-include-paths.html

Upvotes: 0

Related Questions