开发者

Handling relative paths for include files in PHP

I am doing a project in PHP which I am not very familiar开发者_JS百科. 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!


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)


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜