Running PHP Scripts in Command Mode

Most of the time, we write PHP scripts with the idea that they will be run directly in a browser (HTTP). Occasionally, we want to run a script directly from the command line (e.g., in a SSH environment or via a cron job). Unfortunately, running these scripts from the command line can cause problems since most HTTP environment variables are either not available in the command line mode or are not what you might expect them to be. The most common problem this has caused for me is that when I want to include scripts using the include or require commands, I get path problems.

You can read more about using PHP from the command line on the PHP site. While that page told me a lot of useful information about running scripts in command mode, it didn’t actually tell me how to solve my problem of includes not functioning properly in command line mode. Of course, you could just use an absolute path to your include files, but this isn’t a very flexible approach if you decide to change servers, etc. Finally, I did track down a useful piece of code which will do the trick. At the top of your script (or, at least, before any include or require statements) add:


and then make your include statements use paths relative to the script’s directory location, e.g.,


which I presume you are probably already doing…

Since running a PHP script from the command line (PHP CLI) doesn’t change the directory to that of the script, your included files probably won’t be found. The statement above will change the directory and thus your relative paths will function as you originally intended. One warning, however; if your included files further include other files you will probably still have problems so you need to add the command above to each file that has an include or require statement.

Now, I have another warning. As I mentioned at first, many global server environment variables are not available in the command line mode or are different than you might expect. You can find out which ones are available by using the following command line command:

php –i

One thing you can do to deal with this in a fairly easy way is to call your script with the relevant variable you care about. For example, say you want your script to access the HTTP_HOST environment variable. In the regular script you might have:

$host = $_SERVER['HTTP_HOST'];

but that variable won’t be available in command line mode. So, you can call the script like this:

php /path/to/your/script/scriptname

The value will automatically be added to a command line environment variables array, $_SERVER['argv']. The first array value ($_SERVER['argv'][0]) will be the script name you are running and then the next values will be the variables you include in order. So you could change your script to the following:

$host = $_SERVER['argv'][1];

if (!$host) $host = $_SERVER['HTTP_HOST'];

In this case, if the script is run in command line mode with the host variable passed it will run fine, but if it is called by a browser the $_SERVER['argv'] won’t exist and instead we can use our original global environment variable.

My final warning regards calling the script in command line mode. Notice that I used:

php /path/to/your/script/scriptname

I believe that is fine for most server setups, but apparently, some servers will require you to specify the whole path to PHP, like:

/usr/bin/php /path/to/your/script/scriptname

You might also find the following tutorials useful:

Like this content? Why not share it?
Share on FacebookTweet about this on TwitterShare on LinkedInBuffer this pagePin on PinterestShare on Redditshare on TumblrShare on StumbleUpon
There Are No Comments
Click to Add the First »