Nov 20, 2009

How To Get Absolute Path To Website?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

How To Get Absolute Path To Website?

whistle
I am ba beginner to pratice PHP. I tried to create a direction on my website which is hosted on a free hosting server. When I use a function like "mkdir("$credir/$username", 0777);", I can not know the absolute path of my website on the server. May anyone tell me how to know it or other ways to create a directory? Thanks.

Comment/Reply (w/o sign-up)

jedipi
echo $_SERVER['DOCUMENT_ROOT'];
It return the file system path to your root directory by examining a server variable.

echo getcwd();
It return the file system path to the directory that run this command.

echo $_SERVER['PHP_SELF'];
It return the Absolute Path to the file

Comment/Reply (w/o sign-up)

Quatrux
to add, there are lots of ways to get different paths, just do:

CODE
<?php
echo '<pre>';
print_r($_SERVER);
echo '</pre>';
?>


to see what you can do..

or you also can use this function, it is handy sometimes:

realpath();

Comment/Reply (w/o sign-up)

whistle
Thanks for your help! I do get the absolute path by the way you teach me.

I have another problem. As decribed above, I use the function "mkdir("$credir/$username", 0777);", but there is nothing happened. How can I know what's wrong with it? Or how can I debug it?

I put the same scripts on lycos.com and t35.com. The one on lycos.com works, but the other on t35.com does nothing. That is, no error message showed and the page is just refreshed. Of course, the page refreshing could be resulted from some scripts within the page.

Is there any reason or solusion for this problem? Please advise me again. Thank you!

Comment/Reply (w/o sign-up)

Quatrux
strange, i think there is a problem with your variables or path.

the simple code which works just great.
CODE
<?php
mkdir("/path/to/my/dir", 0755);
?>

Comment/Reply (w/o sign-up)

whistle
On Lycos, it show up and error if the absolute path is not correct. On t35.com, no matter the path is, nothing happened and just refreshed.

With your help, I found the absolute path both on Lycos.com and t35.com and then put updated. Lycos.com works, but t35.com fails. I wonder what's difference between them but I am very interested in that.

Anyway, you help me a lot. I feel so lucky to get your help.

Comment/Reply (w/o sign-up)

mastercomputers
Hey whistle,

first find out what your path is:

<?php

echo $_SERVER['DOCUMENT_ROOT'];

?>

e.g. returns /home/username/public_html

It should return your own home path, if it's returning another location that you have no access to, then it's quite possibly a server setup issue of how the server was configured to handle User Directories, you will need to perform another way of getting the location you need.

A good idea is to check if the directory does not exist first before trying to create it.

<?php

$new_dir = 'new_directory';

if(!is_dir($new_dir))
mkdir("$_SERVER['DOCUMENT_ROOT']/$new_dir", 0755);
else
echo 'Directory already exists';

?>

You should not use the octal 0777 for a directory, this maybe a reason why it won't be created as some servers will not permit other users other than the owners having write permissions.

0755 is common for Directories an HTML files, 0750 for CGI/Perl Scripts and 0644 for Text files (because it makes sense that you will not need to execute a text file or want users to alter this file with write permissions).


Cheers,


MC

 

 

 


Comment/Reply (w/o sign-up)

whistle
QUOTE (mastercomputers @ Jul 21 2005, 12:35 PM)
<?php

$new_dir = 'new_directory';

if(!is_dir($new_dir))
mkdir("$_SERVER['DOCUMENT_ROOT']/$new_dir", 0755);
else
echo 'Directory already exists';

?>

*


I tried the above scripts and got
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/freehost/t35.com/u/r/url/config.php on line 22 .

After I tried "echo $_SERVER['DOCUMENT_ROOT'];", I got
/usr/local/apache2/htdocs .

Then I changed above scripts as following:
<?php

$credir = "/usr/local/apache2/htdocs";
$new_dir = 'new_directory';

echo "$credir/$new_dir";
echo $_SERVER['DOCUMENT_ROOT'];

if(!is_dir($new_dir))
mkdir("$credir/$new_dir", 0755);
else
echo 'Directory already exists';
?>

I got as following:
Warning: mkdir(): SAFE MODE Restriction in effect. The script whose uid/gid is 500/500 is not allowed to access /usr/local/apache2/htdocs owned by uid/gid 0/0 in /home/freehost/t35.com/u/r/url/config.php on line 22

I finally know why the directory can't be created, because it is in safe mode. But I still have a question. What is the exact absolute path is?
Is it "/usr/local/apache2/htdocs" or "/home/freehost/t35.com/u/r/url"? which one?

Comment/Reply (w/o sign-up)

calixt
QUOTE (whistle @ Jul 21 2005, 03:02 PM)
I finally know why the directory can't be created, because it is in safe mode. But I still have a question. What is the exact absolute path is?
Is it "/usr/local/apache2/htdocs" or  "/home/freehost/t35.com/u/r/url"? which one?
*


Hi whistle,
can you create a subdirectory with
CODE
$credir="/home/freehost/t35.com/u/r/url/"
?

Which values have $_SERVER["PATH_TRANSLATED"] and $_SERVER["SCRIPT_FILENAME"]?
Are they the same as $_SERVER['DOCUMENT_ROOT']?

Calixt

P.S.: Just found two links which might be helpful to explain the difference between
DOCUMENT_ROOT, SCRIPT_FILENAME, PATH_TRANSLATED:

http://www.phpfreaks.com/PHP_Reference/Pre...Variables/8.php

http://www.php.net/manual/en/reserved.variables.php

Comment/Reply (w/o sign-up)

iGuest-balmydrizzle
Replying to jedipi

No, the $_SERVER['PHP_SELF'] doesn't return that, it only return the URI part, e.G.

If url = 'http://site.Com/abc/def.Php'

$_SERVER['PHP_SELF'] returns '/abc/def.Php', this is definitely not absolute path

(tested under PHP 4.X)

-reply by balmydrizzle

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for absolute, path, website
advertisement



How To Get Absolute Path To Website?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com