Forum Moderators: coopster

Message Too Old, No Replies

access to directories

         

billybob

9:28 pm on Dec 1, 2008 (gmt 0)

10+ Year Member



I guess this is a typical beginner question, but i hope you can help:

i have no success reading a file by pointing to its location via
$path = $_SERVER['DOCUMENT_ROOT']."filedir";

but

$path = "http://localhost\\filedir"; (in a local enviroment)

works.

I have some lack in understanding and therefore i'm wondering myself how can i access a file server sided by using the absolute path to its location?

thanks.

eelixduppy

9:40 pm on Dec 1, 2008 (gmt 0)



1) Are you getting any errors when you try to use the path? 2) echo out the contents of $_SERVER['DOCUMENT_ROOT'] and tell me what it gives you. You should be able to see what's wrong from there.

And Welcome to WebamsterWorld! :)

billybob

9:54 pm on Dec 1, 2008 (gmt 0)

10+ Year Member



Thanks.


1) Are you getting any errors when you try to use the path?

Actually, i'm storing the path to a .swf file in a variable. In a embedded flash object i set the src of the movie to that path.

If i set the path with $_SERVER['DOCUMENT_ROOT'], the .swf is not found, although the path i echo from $_SERVER['DOCUMENT_ROOT'] is correct. BTW, when i enter the path by hand, the flash file does not load either.

But if i use a relative path like ./filedir or the "h t t p://localhost..." path, the .swf loads.

Besides that, i do not get any errors.

vincevincevince

10:54 am on Dec 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_SERVER['DOCUMENT_ROOT'] is a server path; a file address. What you need is $_SERVER['HTTP_HOST'].

billybob

1:42 pm on Dec 2, 2008 (gmt 0)

10+ Year Member



$_SERVER['HTTP_HOST'] also does not work. :-(

coopster

4:51 pm on Dec 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Depending on your HTTP server type and setup, certain $_SERVER superglobal indexes may or may not be available. You can check to which is readily available by dumping the entire superglobal to the browser:
<?php 
print '<pre>';
print_r($_SERVER);
print '</pre>';
?>

You'll probably need to use the $_SERVER['SERVER_NAME'] value.

... on a side note, there is a similar discussion running right now ...
[webmasterworld.com...]

toplisek

5:37 pm on Dec 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have seen that PHP version 5.2.6 or Apache 2 version has completely new path system.
Path variable
$_PATH1 = "http://" . $_SERVER['HTTP_HOST']."/";

and
<?PHP include($_PATH1.'en-US/footer/'."footer_si.php");?>
They actually do not work any more.

If I go directly to this path (echo path), it will work file.

So, I think this is issue on server side when upgraded.
Is this correct?

coopster

6:10 pm on Dec 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It is neither a PHP issue nor an Apache issue, it is a coding issue. Certain $_SERVER superglobal indexes will always be present, others may or may not be, depending on the server-side configuration and/or the browser request. Dump the $_SERVER array as noted earlier and you will discover which variables are present for your current configuration.

toplisek

7:50 pm on Dec 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<? $_SERVER=$_SERVER['SERVER_NAME'];

print '<pre>';
print_r($_SERVER);
print '</pre>';
?>
Is this correct to show $_SERVER superglobal indexes?
Or what is correct code?

jatar_k

7:54 pm on Dec 2, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no that wouldn't be quite right

print '<pre>';
print_r($_SERVER);
print '</pre>';

just the above would be adequate for dumping the $_SERVER superglobal

billybob

10:13 pm on Dec 2, 2008 (gmt 0)

10+ Year Member



finally i found a solution to my problem. :)

I had to set $HTTP_REFERER = $_SERVER['HTTP_REFERER'];

as well

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

The correct source path is then $DOCUMENT_ROOT.$image;
for instance.

toplisek

8:53 am on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi jatar_k,
thank you. What is now correct procedure to determine correct path?

I have printed this infomation from server.

Need help.

toplisek

9:21 am on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



which $_SERVER superglobal indexes is correct to see in this print that path will be correct determined all the time?

vincevincevince

2:49 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_SERVER['DOCUMENT_ROOT'] is always the server path (e.g. /var/www/html/something/php)
$_SERVER['HTTP_HOST'] is always the domain name type path (e.g. http://www.example.com/)
$_SERVER['REQUEST_URI'] is always the requested path (e.g. /scripts/hello.php

You just need to figure out which one you need, for example, an embed link to a .swf should be the HTTP_HOST based link - but a php include should be based on DOCUMENT_ROOT

toplisek

3:01 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
i'm not sure if your is 100% correct.
I have posted result to server and he folowing is true fo my server:
$_SERVER['HTTP_HOST'] http://www.example.com
NOT http://www.example.com/ notice / the end.

Will depend also other specification within $_SERVER superglobal index for last character like /

vincevincevince

3:26 am on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, to clarify:

$_SERVER['HTTP_HOST'] should be just www.example.com (not http:// and not ending in /

Are you sure you get http://www.example.com ?

What I meant to say was that you use the HTTP_HOST when you need to make a fully qualified URL:

print "http://$_SERVER[HTTP_HOST]/";

toplisek

7:48 am on Dec 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi
you are correct. I made also mistake.
HOST variable is the following: www.mydomain.com

Is print "http://$_SERVER[HTTP_HOST]/"; the same as my code:
$_PATH1 ="http://" . $_SERVER['HTTP_HOST']."/";

upgraded to Apache version 2.0.63. and also to PHP version 5.2.6
Strange that on new server this path does not work anymore.

Do you have any idea which index variable should be additional checked?

toplisek

2:58 pm on Dec 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
I have seen that on new server is changed:
New server
[REQUEST_URI] => /
[SCRIPT_NAME] => /index.php

Old server showed me:

[REQUEST_URI] => /index.php
[SCRIPT_NAME] => /index.php

What will influence this variable? Need help.
I had in old server:$_PATH1 ="http://" . $_SERVER['HTTP_HOST']."/";

Now it does not work anymore path.