PHP 在 Mac 上包含的正确路径是什么?

发布于 2024-08-25 05:07:58 字数 1075 浏览 5 评论 0原文

运行 Mac OS X 10.5.8,预装 PHP 5.2.11。使用 Coda 1.6.10。

我正在编写 PHP 文件,然后预览它们从文件而不是服务器运行。在我尝试 PHP include 之前,这一切都工作得很好。它们不能作为相对路径,只能作为驱动器根目录的绝对路径。

有什么方法可以使用类似的语句

include_once“common/header.php”;

不指定我的整个文件路径,如下所示:

include_once“/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0/common/base.php”;

,其中 ColoredLists_v1.0 是包含所有网站文件的目录。我尝试了在文件路径前面添加 _SERVER[DOCUMENT_ROOT] 或 dirname(File) 等解决方案,但这不起作用,因为未设置变量。有没有简单的方法可以做到这一点,或者我可以更改配置,以便它默认在特定目录中查找而不是查看驱动器根目录?目前,echo_include_path 显示 .:

当我在脚本开头包含此行时,它可以工作:

set_include_path('/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0');

但是,如果我想对所有脚本执行此操作,我似乎无法使更改永久化。即使我在 php.ini 中编辑了 Unix include_path 后,它似乎也不起作用。

更新:我最终决定最简单的方法是在本地运行 Apache 服务器。它预装在 Mac 上,所以没有遇到太多麻烦。我修改了 httpd.conf 以仅允许来自本地主机的连接,从而减轻了我的安全担忧。当然,这仍然打开端口 80,但我在路由器后面,所以我希望我不会太暴露。 Coda 在“站点”中包含一个很棒的功能,我可以在其中设置本地 url,所以它现在运行得非常好。唯一的缺点是,现在,在代码编辑后,我需要手动刷新预览。

Running Mac OS X 10.5.8, with PHP 5.2.11 Pre-installed. Using Coda 1.6.10.

I'm writing PHP files, and then preview them running from file, not server. This was working fine till I tried PHP includes. These don't work as a relative path, only as an absolute from the root of the drive.

Is there any way I can use statements like

include_once "common/header.php";

without specifying my entire file path like so :

include_once "/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0/common/base.php";

,where ColoredLists_v1.0 is the directory with all the website files in it. I tried solutions like prepending _SERVER[DOCUMENT_ROOT] or dirname(File) to the file paths, but that didn't work as the variables were not set. Is there any easy way to do this, or a configuration I can change so that it looks in a specific directory by default instead of looking at the drive root? Currently, echo_include_path shows .:

When I include this line at the start of the script, it works:

set_include_path('/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0');

However, if I want to do this for all my scripts, I can't seem to make the change permanent. Even after I edited the Unix include_path in my php.ini, it doesn't seem to work.

UPDATE: I finally decided that the easiest sureshot way was to run the Apache server locally. It comes preinstalled with Mac, so didnt face much trouble there. I modified the httpd.conf to only allow connections from localhost, thus alleviating my security fears. Ofcourse, this still opens up port 80, but I'm behind a router, so I'm gonna hope I'm not very exposed. And Coda includes a great features in 'Sites', where I can set the local url, so it works brilliantly now. Only downside is, now, after code edits, I need to manually refresh the preview.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

古镇旧梦 2024-09-01 05:07:58

你可以尝试这个方法。

require_once(dirname(__FILE__) . '/example.php');

You can try this method.

require_once(dirname(__FILE__) . '/example.php');
青巷忧颜 2024-09-01 05:07:58

尝试

set_include_path(
    '/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0' .
    PATH_SEPARATOR . 
    get_include_path());

使用共享包含文件

或编辑 php.ini 并更改 include_path 变量(我正在使用 MAMP,因此无法告诉您 php.ini 的确切路径)

Try

set_include_path(
    '/Volumes/Macintosh HD/Users/neil/Desktop/Website/ColoredLists_v1.0' .
    PATH_SEPARATOR . 
    get_include_path());

in a shared include file

or edit your php.ini and change the include_path variable (I'm using MAMP so can't tell you the exact path to php.ini)

马蹄踏│碎落叶 2024-09-01 05:07:58

这确实是一个 include_path 问题。默认似乎是 .:/usr/lib/php;也许如果您从脚本所在的目录启动 PHP 命令,那就可以了?

It truly is an include_path issue. The default seems to be .:/usr/lib/php; perhaps if you launched the PHP command from the directory in which are your scripts, it would be fine?

心不设防 2024-09-01 05:07:58

这对我来说每次都有效:

this works every time for me:

<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/common/header.php'); ?>

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