Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将文档根目录定义为根路径_Php_Constants_Document Root - Fatal编程技术网

Php 将文档根目录定义为根路径

Php 将文档根目录定义为根路径,php,constants,document-root,Php,Constants,Document Root,我使用一些常量来定义我正在构建的网站上特定目录的路径,如下所示: define("ROOT_PATH", "http://website.com/"); define("IMAGES_DIR", ROOT_PATH . "images/"); 我通常这样使用它们: echo "<img src='" . IMAGES_DIR . "some_image.jpg' />"; 及 我应该使用哪一种?为什么呢?您不能真正使用以下内容: define("ROOT_PATH", "/hom

我使用一些常量来定义我正在构建的网站上特定目录的路径,如下所示:

define("ROOT_PATH", "http://website.com/");
define("IMAGES_DIR", ROOT_PATH . "images/");
我通常这样使用它们:

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


我应该使用哪一种?为什么呢?

您不能真正使用以下内容:

define("ROOT_PATH", "/home/username/public_html/");
因为它将尝试加载

http://website.com/home/username/public_html/image.png
你并不真正想要的

使用

我会设法去拿

http://website.com/image.png
这就是你想要的。

你应该使用

define("ROOT_PATH", "http://website.com/");
如果您使用:

define("ROOT_PATH", "http://website.com/");
在客户端,映像src:

http://website.com/some_image.jpg
http://website.com/home/username/public_html/some_image.jpg
如果使用:(仅适用于本地文件系统)

图像src:

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

区别在于html无法识别图像或src属性上的完整路径。。。但是你可以用这个

define ('ROOT', dirname(dirname(__FILE__)));
define ('ROOT_PATH', "http://website.com/");
define ('INCLUDES',ROOT .'path/to/php/includes/'); //for php includes
或者,您可以使用php函数根据getenv(“脚本名称”)转换“根路径”

尝试将图像链接到,看看是否有效;)
http://website.com/home/username/public_html/some_image.jpg
define ('ROOT', dirname(dirname(__FILE__)));
define ('ROOT_PATH', "http://website.com/");
define ('INCLUDES',ROOT .'path/to/php/includes/'); //for php includes