Defining the document root directory as the ROOT_PATH

泄露秘密 提交于 2019-12-12 05:24:06

问题


I use some constants to define paths to specific directories on a website that I'm building, like this:

define("ROOT_PATH", "http://website.com/");
define("IMAGES_DIR", ROOT_PATH . "images/");

and I usually use them like this:

echo "<img src='" . IMAGES_DIR . "some_image.jpg' />";

now I want to know if there is any difference between

define("ROOT_PATH", "http://website.com/");

and

define("ROOT_PATH", "/home/username/public_html/");

and which one of them should I use? And why?


回答1:


You can't really use the following:

define("ROOT_PATH", "/home/username/public_html/");

since it will try to load

http://website.com/home/username/public_html/image.png

which you don't really want.

Using

define("ROOT_PATH", "http://website.com/");

will try to fetch

http://website.com/image.png

which is what you want.




回答2:


the difference is that html won't recognize full paths on images or src attributes... but you can use this

define ('ROOT', dirname(dirname(__FILE__)));
define ('ROOT_PATH', "http://website.com/");
define ('INCLUDES',ROOT .'path/to/php/includes/'); //for php includes

Or you can use a php function to convert "ROOT_PATH" based on the getenv("SCRIPT_NAME")




回答3:


You should use

define("ROOT_PATH", "http://website.com/");

If you use:

define("ROOT_PATH", "http://website.com/");

In client side,The image src:

http://website.com/some_image.jpg

If you use:( Just for local file system )

define("ROOT_PATH", "/home/username/public_html/");

The image src:

http://website.com/home/username/public_html/some_image.jpg


来源:https://stackoverflow.com/questions/14192210/defining-the-document-root-directory-as-the-root-path

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