Search code examples
phpdirectory-structure

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 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?


Solution

  • set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));