如何获得当前PHP文件相对于Web根文件夹而不是系统根文件夹的路径?

发布于 2025-02-13 20:52:19 字数 798 浏览 0 评论 0原文

几年

define('APP_ROOT', dirname(__FILE__) . DIR_SEP);

前Web根本身,因此该应用具有一个基本路径,可以动态计算应用程序中使用的任何其他路径。

当时我的测试系统是在Windows 7上运行的XAMPP安装。我不再具有该系统,但我相信HTDOCS文件夹的路径是c:\ xampp \ htdocs,我的应用程序包含在c:\ xampp \ htdocs \ timesheets。在该系统上,app_root的值是时间表/,完全按照我的意愿。

我知道客户在Linux服务器上运行了该应用程序,并且我相信该应用程序是在/var/www/timesheets/中安装的。因此,app_root的值是时间表/。我知道这起作用了,因为他永远不需要修改我的代码。

我现在正在尝试再次使用相同的代码,但是我的测试系统是Ubuntu 20.04,Apache 2.4和PHP 7.4。我在app_root中获得的值是/var/www/html/timesheets/,这导致我所有的动态路径计算是错误的。

我已经尝试设置doc_rootopen_basedirphp.ini中,但我仍在获得完整的绝对路径。我认为这必须与服务器或PHP配置有关,但是我搜索了网络,找不到如何纠正它。

Some years ago I developed a web app which contained the following code in a php file located in the root folder of the app:

define('APP_ROOT', dirname(__FILE__) . DIR_SEP);

This was to allow the app to be installed in any sub directory of the web docs folder and not necessarily directly in the web root itself, so that the app had a base path from which to dynamically calculate any other paths used in the app.

My test system back then was an Xampp installation running on Windows 7. I no longer have that system, but I believe the path to the htdocs folder was C:\xampp\htdocs and my app was contained in C:\xampp\htdocs\timesheets. On that system, the value of APP_ROOT was timesheets/, exactly as I wanted.

I know that the customer ran the app on a Linux server and I believe the app was installed in /var/www/timesheets/. So the value of APP_ROOT was timesheets/. I know this worked correctly because he never needed to modify my code.

I am now trying to use the same code again, but my test system is Ubuntu 20.04 with Apache 2.4 and PHP 7.4. The value I am getting in APP_ROOT is /var/www/html/timesheets/, which is causing all my dynamic path calculations to be wrong.

I have tried setting doc_root and open_basedir in php.ini, but I am still getting the full absolute path. I think this must be to do with the server or php configuration, but I have searched the web and cannot find how to correct it.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文