开发者

Problem with $_SERVER['DOCUMENT_ROOT']

I have a simple problem with the server global variable named : DOCUMENT_ROOT.

Here's my config.php file :

<?php
define ('UPLOAD_DIR',$_SERVER['DOCUMENT_ROOT'].'/resouces/');

The test.php file

<?php

$photo='myi开发者_StackOverflow中文版mage.php';
echo '<img src="'.UPLOAD_DIR.$photo.'">';

The problem is that when testing the code.. The image doesn't display, and the directory starts with :

C://program files/easyPHP/www/resources

instead of

http://127.0.0.1:8888/

As it supposed to be... I tried to update this line in the httpd.conf file :

DocumentRoot "${path}/www"

to

DocumentRoot "http://127.0.0.1:8888/www"

But the server declares an error :

Problem with $_SERVER['DOCUMENT_ROOT']

Thank you in advance :-)


You're misunderstanding DOCUMENT_ROOT; it's the path to the root directory that code is served from, not an IP, which is why you're getting the error in the screenshot. Also, when you're uploading files, you actually want the file path, not the server IP.

From the docs:

[Document root is] The document root directory under which the current script is executing, as defined in the server's configuration file.

When you put paths to files in HTML, it's very very bad practice to use the server's IP; it should be a file path. It's much better to use a relative file path instead of an absolute one; it reveals much less about your site to the public, making it a bit more secure.


You are confusing between Physical Path and WWW URL.


$_SERVER['DOCUMENT_ROOT'] is path on your real filesystem. You need not it here. Just use

define ('UPLOAD_DIR','/resouces/');

Don't mix up path on filesystem and path on your site.
They may have nothing common (but in default case requested URI sent to $_SERVER['DOCUMENT_ROOT'].URI)

You can also use

define ('UPLOAD_DIR_FILEPATH',$_SERVER['DOCUMENT_ROOT'].'/resouces/'); //to use in upload, renaming, deleting etc
define ('UPLOAD_DIR_WWW','/resouces/'); //to show URLs

You need not declare it on every levelb ecause path starts from root /

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜