Jump to content

[SOLVED] DOCUMENT_ROOT


jaymc

Recommended Posts

I use $_SERVER['DOCUMENT_ROOT'] to check the path of a script however I have just realised if you run a script outside of apache, $_SERVER variable does not contain anything

 

So my question is, when running php script via cronjob or command line how can I retreive the document root of the file?

Link to comment
Share on other sites

well...what are you trying to accomplish exactly...you have a couple options:

 

getcwd() - Gets the current working directory

dirname(__FILE__) - Gets the directory the current file is in

 

edit: __FILE__ is a global variable by the way, not a typo

Link to comment
Share on other sites

Wait sorry it doesn't do what I need

 

<?
// this php file exists in /home/jay/live/www/scripts/file.php
// I want $dir to = /home/jay/live/www/scripts/
// not /home/jay/

include("/home/jay/global.php"); // this has $dir = dirname(__FILE__);

echo $dir;
?>

Link to comment
Share on other sites

There is no good way to determine a DOCUMENT_ROOT in this situation. Your global.php is outside of your site's directory. You could always do this:

$dir = dirname(__FILE__).'/live/www/scripts/';

 

or how about:

$dir = dirname(realpath($_SERVER['SCRIPT_NAME']));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.