如何获得当前PHP文件相对于Web根文件夹而不是系统根文件夹的路径?
几年
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_root
和open_basedir
在php.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论