$_SERVER[DOCUMENT_ROOT] in CLI or Cron

$_SERVER['DOCUMENT_ROOT'] will not be available in the CLI . The web server defines the document root. In the CLI , you are not using a web server, so there is no document root.

You can try to rely on environment variables, assuming they are set by your shell. For example, $_SERVER['PWD'] represents the current directory and $_SERVER['HOME'] represents the user's home directory.

But this example with PWD is irrelevant if you run the script from a different directory than the executable file. That is, PWD reflects the directory in which you are at the time of launch.

I will give my example , which allows you to calculate the directory of the file being launched:

<?php
$self = pathinfo(__FILE__, PATHINFO_BASENAME);
$document_root = rtrim(str_replace($self, '', __FILE__), '/');

$document_root will contain the directory where the executable file is located.

4418 0

Comments

Be the first to review this item. Share your rating and review so that other customers can decide if this is the right item for them.
You have to log in to leave a comment. Sign in

Similar articles

Create and download CSV in PHP

Consider the possibility of quickly creating a CSV file with automatic file download. Consider the formation, separators and header for the ability to download the file.