简体   繁体   English

网站的根目录在PHP的子目录中

[英]Root of website in a subdirectory in PHP

I'm developing a website in PHP. 我正在用PHP开发一个网站。 The test environment is in a subdirectory of the root and there are multiple test environments. 测试环境位于根目录的子目录中,并且有多个测试环境。 But the website doesn't work properly because of al the absolute and also the relative paths. 但是由于绝对路径和相对路径,该网站无法正常运行。 So i need a clean solution to get the path of the root of the website and not of the server. 因此,我需要一个干净的解决方案来获取网站而不是服务器根目录的路径。

I've tried a lot of thinks but nothing seems to work. 我已经尝试了很多想法,但是似乎没有任何效果。

I hope you have a beter solution for this problem. 希望您对此问题有更好的解决方案。

Thanks in advance! 提前致谢!

Not sure if this will help, but if you want to get directory of current php file you should use __DIR__ constant. 不知道这是否有帮助,但是如果您想获取当前php文件的目录,则应使用__DIR__常量。 More info here . 更多信息在这里

If your website index is located at /home/www/subpath/index.php, this code in index.php: 如果您的网站索引位于/home/www/subpath/index.php,则index.php中的以下代码:

echo __DIR__;

will return: 将返回:

/var/www/subpath

You can use a SESSION to set your desired PATH and use it on all the path you need. 您可以使用SESSION来设置所需的PATH并将其用于所需的所有路径。

If you are using a framework like Laravel this can be done a lot easier. 如果您使用的是Laravel之类的框架,则可以轻松得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM