简体   繁体   English

将文档根目录定义为ROOT_PATH

[英]Defining the document root directory as the ROOT_PATH

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? 为什么?

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. 这就是你想要的。

the difference is that html won't recognize full paths on images or src attributes... but you can use this 区别在于html无法识别图像或src属性的完整路径......但您可以使用它

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") 或者您可以使用php函数根据getenv(“SCRIPT_NAME”)转换“ROOT_PATH”

You should use 你应该用

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

If you use: 如果您使用:

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

In client side,The image src: 在客户端,图像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: 图像src:

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

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

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