PHP脚本的目录结构管理
我正在编写一个基于配置文件的图像下载器,它由多个文件构成:
lib/
profiles/
getbooru
install
readme
uninstall
如果我像这样包含这些文件:
include 'lib/curl';
include 'lib/string';
foreach(glob('profiles/*') as $profile)
include $profile;
该程序仅在您从程序目录中调用它时才有效。如果我将其更改为:
set_include_path('/usr/local/share/getbooru');
include 'lib/curl';
include 'lib/string';
foreach(glob('/usr/local/share/getbooru/profiles/*') as $profile)
include $profile;
然后它会强制用户运行安装脚本以将文件放入该目录中,并且它在其他任何地方都不起作用。在这种情况下,getbooru
符号链接到 /usr/local/bin/getbooru
。
好消息:我注意到,如果我尝试获取正在运行的脚本的文件名,即使它是通过符号链接运行的,它也总是会返回“真实”脚本名称(因此我不会' t 包括 /usr/local/bin
中不存在的内容)。
我怎样才能使这个程序可移植到任何地方运行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我见过的方法是从配置脚本中获取基本路径并将其存储在应用程序配置文件中,例如 global $base_path; $base_path='/var/www/myapp/';。然后你像这样包含
global $base_path;包括 $base_path.'lib/curl';
。在执行任何其他包含操作之前,必须小心包含配置文件。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 thisglobal $base_path; include $base_path.'lib/curl';
. You have to be careful to include the config file before you do any other includes.