PHP脚本的目录结构管理

发布于 2024-09-24 20:23:06 字数 790 浏览 0 评论 0原文

我正在编写一个基于配置文件的图像下载器,它由多个文件构成:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-10-01 20:23:06
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
娇妻 2024-10-01 20:23:06

我见过的方法是从配置脚本中获取基本路径并将其存储在应用程序配置文件中,例如 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文