Modify PHP CLI Include Path Dynamically

I was working with an application platform that I had just downloaded today. I just wanted to check it out and play with it a bit. In the application framework was an executable script that provided some command line utility. I fired up the script and received an error like “PHP Fatal error: require_once(): Failed opening required…”. Of course non of the files from the downloaded archived were in the include path for my system, but I didn’t want to just go adding it to my php.ini file just to play around with it short term. What to do.

Command Line Options

First I wanted to see if there was a command line option that I could pass to PHP to tell it what include_path to use for just this one execution. If you execute the following you’ll see the usage:
php --help
There is no include_path command line option, but there is the -d foo[=bar] option which allows us to modify any of the INI entries by key/value.

A Little Parsing

Now all we need to do is combine the -i option, which lists a bunch of information about PHP including the INI entries, with some parsing and we can get the current include_path value. This command will show us the current value:

Of course we’ll need to get just the value if we want to modify it:

In the awk script above you’ll see that we set the FS (field separator) system variable to split up the line we get from the php -i | grep include_path call. We also take advantage of the NF (number of fields) system variable to print out the last field, which happens to be the value of include_path.

Putting It All Together

Using this, we can now construct our own value to pass in to the include_path for PHP.

Note that instead of using the print command in our awk script we use printf instead. This just prevents awk from outputting a newline after it prints our data. Also note the use of back tics so that we can assign the output of an execution to a variable. We then append our own path to that and we have our new include_path.

  • Jason

    You have no idea how much this post helped me out. THANK YOU!

  • Glad it helped! and thanks for commenting.

  • Chintan

    Me too…. Thanks Jonathan

  • Shimon

    Man, you are genius! Thanks a lot!

  • Alvaro

    Thank you so much, this was a really helpfull tip to change the include_path in environment like dreamhost were there is no easy way to modify the php.ini file for php-cli.

  • Hi Jonathan, just wanted to say Thank You for this old post! It did exactly what I needed to support some legacy PHP CLI scripts that expected its include_path to be specific to an old environment.

  • Jesús

    Thanks dude!