Handling relative paths for include files in PHP

不想你离开。 提交于 2020-01-06 09:56:11

问题


I am doing a project in PHP which I am not very familiar. I am using a MVC framework (CodeIgnitor). I have noticed that each time I return a view that resulted from a longer/shorter url string all of my includes break. It appears that the paths are relative to url.

Is $_SERVER["DOCUMENT_ROOT"] the best way to generate include paths in PHP?

Thanks!


回答1:


base_url() and site_url() is probably what you need.




回答2:


You should read the User Guide on URL Helpers. It already has all the infos you need and provides with with functions that give you paths for your site.

If you need paths on your file system, there are BASEPATH, APPPATH and FCPATH. Look into index.php to see where they point (also has a description of these constants)




回答3:


I really depends on your application, I don't know how CodeIgnitor works, but here are a few points:

If you use the php path (defined in php.ini) you can always keep you includes in the php path, so including a file is no longer relative to the file path.

If you have a project directory (like /srv/www/myProject/) and all files you are using reside in this dir, then you could define a session value like this $_SESSION['project_path'] = '/srv/www/myProject' and then when including files, it would look like this:

include_once($_SESSION['project_path'] . 'included.php');

Calling the absolute path would make the include not care about the current path.



来源:https://stackoverflow.com/questions/3242975/handling-relative-paths-for-include-files-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!