2

Simply put, how do I specify an absolute filesystem path using the require_once keyword in PHP? The code will be run in CLI, not on a webpage. What is the root file path? I am running Windows, FYI.

Thank you.

user717236
  • 4,959
  • 19
  • 66
  • 102

4 Answers4

7

Try using a Magic Constant __DIR__

__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)

Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
3

On windows, it's the backslash notation, also you have to escape them if you are using double quotes.

require_once("C:\\folder\\folder\\etc");
2
require_once('/some/path/foo.php');

Starting the path with a / indicates you are using a absolute (starting in root) path.

require_once('some/path/foo.php');

Starting the path without a / indicates you are using a absolute (starting in current working dir) path.

Otherwise, have a look at the __FILE__ and __DIR__ magic constants.

RobIII
  • 8,488
  • 2
  • 43
  • 93
  • 3
    Whoever downvoted this: at least have the decency to explain *why* – RobIII Jul 18 '12 at 15:42
  • 1
    They certainly should have explained - I am now left wondering if your answer works on Windows, because that would be my assumption as to why it was downvoted. – bcmcfc Jul 18 '12 at 16:15
1

Well, \ alone will get you the root of the current drive (typically C:\), or if you need to you can specify which drive you need. Just remember to escape those backslashes, or use forward slashes instead (Windows will silently convert them)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592