Directory structure management for PHP script
I'm writing a profile-based image downloader, and it's structured in multiple files:
lib/
profiles/
getbooru
install
readme
uninstall
If I include these files like this:
include 'lib/curl';
include 'lib/string';
foreach(glob('profiles/*') as $profile)
include $profile;
The program only works if you call it from the program's directory. If I change it to this:
set_include_path('/usr/local/share/getbooru');
include 'lib/curl';
include 'lib/string';
foreach(glob('/usr/local/share/getbooru/profiles/*') as $profile)
include $profile;
Then it forces the users to run an install script to put the files in that directory, and it won't work anywhere else. In开发者_StackOverflow中文版 this case, getbooru
is symlinked to /usr/local/bin/getbooru
.
Good news: I noticed that if I try to get the filename of the running script, even if it's running through a symlink, it'll always return the 'real' script name (so that I don't include nonexistent stuff from /usr/local/bin
).
How can I make this program portable to run anywhere?
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
The approach I've seen is to grab the base path from a configure script and store it in the apps config file, e.g. global $base_path; $base_path='/var/www/myapp/';
. Then you include like this global $base_path; include $base_path.'lib/curl';
. You have to be careful to include the config file before you do any other includes.
精彩评论