1

Is there a way to disable/negate the use of $_SERVER['DOCUMENT_ROOT'] when including my php files? I would prefer

require '/somedirectory/somefile.php'; 

instead of

$_SERVER['DOCUMENT_ROOT'].'/somedirectory/somefile.php';

Running Centos and cPanel.

Or is this something that will always be required when writing my php code?

2
  • If you're sure that your code will run without it and you're happy to change the references when/if your path changes then you can totally remove the $_SERVER['DOCUMENT_ROOT'] from your code. Commented Feb 17, 2017 at 20:59
  • That will depend on if you plan on packaging this for distribution. Commented Feb 17, 2017 at 21:06

3 Answers 3

2
require './somedirectory/file.php 

works from the base of the application without explicitly referring to the $_SERVER ... There is no disabling but there is defining that variable or different variables to aid in your routing. variables like base_url and site_url

The dot refers to the root of the application.

Sign up to request clarification or add additional context in comments.

2 Comments

upvoted your answer as I can't have two accepted answers. The dot worked perfectly thanks. It's the little things you always miss.
really glad it worked, I forgot to mention set include paths but the simplest way is the dot notation
1

Just use:

require './somedirectory/somefile.php'; 

1 Comment

O I see, you put a . in front of it. I missed that completely! Thanks , That fixed the issue!
0

It's not required that you use those. You don't have to use full paths. Apache and PHP will generally look in the same path as the calling file (omit the / or PHP will think you mean the root of the server drive)

require 'somedirectory/somefile.php'; 

Another option is to set the include_path so PHP knows where to look implicitly

set_include_path('/full/path/to/files');

1 Comment

see comments above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.