Define a rootpath in php

倖福魔咒の 提交于 2019-12-12 04:58:29

问题


I read up on how to define a rootpath constant that is useable in all subpages when using a single page application.

Now my question is, how can i use the basepath in my scripts i point to in my form actions?

My problem is that i want to use a relative path, compared to where the script is located, in the "action" scripts so that the path will work no matter from where that file is called.

I tried using

dirname(__FILE__) 

but as i use files in parent directories some levels up i ended up with something like

dirname(dirname((__FILE__))

and sometimes even another level up and i did not like the look of that.

After trying to to use a basepath constant i found out that it is not accessable in the scripts my forms are calling on form submit.

So my question is. How can i use a common basepath that works in the entire application? I was thinking about storing it in a session, but that seemed like a bad idea as it has no real place there.

Any advice would be appreciated, thanks in advance.


回答1:


There is no common basepath for both the client- and the server-side of your application.

On the client side, all paths are relative to the current url or absolute which is relative to the base url / domain name.

On the server side your are dealing with the file-system.

So if your site root on the server-side ($_SERVER["DOCUMENT_ROOT"]) is something like /home/some-user/www and your form processor is located at /home/some-user/www/assets/inc/process-form-1.php, that is the exact path you need on the server-side (if you want to use absolute paths...).

However, on the client-side, your action would point to a relative path or an absolute path which in this case would be /assets/inc/process-form-1.php; an absolute path relative to the root of the web-server.




回答2:


The most usual solution is

dirname(dirname(__FILE__))

It is the most reliable solution, dont forget you can always go backwards as well

dirname(dirname(__FILE__)).'/../'


来源:https://stackoverflow.com/questions/17574224/define-a-rootpath-in-php

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