Jump to content
xisto Community
Sign in to follow this  
whistle

How To Get Absolute Path To Website?

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

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

to see what you can do..

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

realpath();

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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

the simple code which works just great.

<?phpmkdir("/path/to/my/dir", 0755);?>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Hey whistle,first find out what your path is:<?phpecho $_SERVER['DOCUMENT_ROOT'];?>e.g. returns /home/username/public_htmlIt 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);elseecho '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

Share this post


Link to post
Share on other sites

<?php

 

$new_dir = 'new_directory';

 

if(!is_dir($new_dir))

mkdir("$_SERVER['DOCUMENT_ROOT']/$new_dir", 0755);

else

echo 'Directory already exists';

 

?>

 

<{POST_SNAPBACK}>


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?

Share this post


Link to post
Share on other sites

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?

<{POST_SNAPBACK}>

Hi whistle,

can you create a subdirectory with

$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://forums.xisto.com/no_longer_exists/

 

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

Edited by calixt (see edit history)

Share this post


Link to post
Share on other sites
Replying to jedipiNo, the $_SERVER['PHP_SELF'] doesn't return that, it only return the URI part, e.G.If url = 'https://www.salesforce.com/products/platform/overview/'] returns '/abc/def.Php', this is definitely not absolute path (tested under PHP 4.X)-reply by balmydrizzle

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.