SiteCrafting Blah Blah Blog
Apr. 18, 2007 at 1:08pm
PHP PEAR and include_path
When attempting to use the SMTP library from PEAR for a project, I was having problems with including the file. I kept getting a file stream error for this code:
require 'Net/SMTP.php';I figured the problem had to be related to the include_path setting in the configuration file.
When PEAR is installed, the include path in the PHP ini file is updated with the path to PEAR. Also, there's another path to the php folder, which is used by PEAR. So, if it's missing, PEAR includes will not work.
Here's an example of the master include path:
path = .:/usr/share/php:/usr/share/pear However, in the configuration, the ini_set( "include_path", "../include/"); overrides that setting, which prevents the require() function from working. At first, I figured I only needed to add the path for pear, so I tried setting the include_path this way.
ini_set("include_path","/usr/share/pear:../include/" );But that include failed also. That's when I figured out that PEAR also uses the "/usr/share/php" path and possibly the "." path, which is the path for the current directory.
Being the lazy programmer that I am, I didn't want to have to worry about updating these paths for different environment and settings, so I used this code snippet instead.
$strMasterInclude = get_include_path();
ini_set( 'include_path', "{$strMasterInclude}:../include/");Now, the paths required for PEAR will always be included, no matter the environment.
NOTE:
In this example, the file paths are separated by a colon ":", but the paths could also be separated by a semi-colon ";", depending on your setup.
Posted in Coding Techniques, From the Workbench, PHP by Ken Foubert
Comments (0)
Add your comment below