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:
php -i | grep include_path
include_path => .:/usr/local/lib/php:/usr/local/php5/lib/pear => .:/usr/local/lib/php:/usr/local/php5/lib/pear
Of course we’ll need to get just the value if we want to modify it:
php -i | grep include_path | awk 'BEGIN{ FS=" => " }{ print $NF }'
.:/usr/local/lib/php:/usr/local/php5/lib/pear
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.
#!/bin/bash
INCPATH="`php -i | grep include_path | awk 'BEGIN{ FS=" => " }{ printf($NF) }'`:/my/custom/path"
echo "Using include_path = ${INCPATH}"
php -d include_path=${INCPATH} my_php_script.php
exit 0
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
.