2

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='myimage.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 :

enter image description here

Thank you in advance :-)

2
  • 1
    That is what it is supposed to do... Also, don't allow uploads directly to anything under the document root. You are opening yourself to world a security troubles if you do this. Commented Aug 21, 2011 at 14:37
  • Thank you brad ... I will really avoid'em +1 Commented Aug 21, 2011 at 14:52

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But what's the solution then?? and why my images are not displaying?
$_SERVER['REQUEST_URI'] or you can work with $_SERVER['PATH_INFO']. See: php.net/manual/en/reserved.variables.server.php Those will help you get the currently running script. If you want the doc root, just use /. That's where the doc root will be on the web side of things.
2

You are confusing between Physical Path and WWW URL.

4 Comments

I really know the difference between them, my images are displaying the C:// path, and I want the 127.0.0.1 path ,or localhost :-)
In a previous version of apache , I was using the DOCUMENT_ROOT variable for the WWW URL , not the physical path... this is the first time I had this error
Just use echo '<img src="/resource/'.$photo.'">';
I have one config.php file... and many directories with different levels ... I have to use this config.php file to point to the same directory (uploads) from all those directories.. so , if I host the project , I will change the resources directory from one file :-)
1

$_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 /

4 Comments

The problem Riad is that I use this resources file in many directories with different levels ... so do I have to declare a constant for every level???
@Simo TAQI, you need not constant to every level, see updated answer
Thank you, but Brad said me that using $_SERVER['DOCUMENT_ROOT'] in uploads my have some troubles on security. But you're solution is still working +1
Thank you friends ... it's now working , I removed the Document_root from the code that shows URLs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.