Jump to content
  • Advertisement
Sign in to follow this  
Lantz

[web] apache+php+paths

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a way to make the paths I use in php code (to read and write files for example) to correspond to the webpage's paths? For example if I do fopen( "/shite/test.dat", "rb" ) I want it to open /shite/test.dat relative to the root of the webpage, not the root of the server like it does as I have it now. Im kinda new to php and apache and have probably just not configured it properly. I have managed to get the includes working by setting the include_path or whatever its called in php.ini, but I dont like this solution. Any suggestions or good links on the subject? Lantz

Share this post


Link to post
Share on other sites
Advertisement
There is a function for this in ASP, called MapPath. It maps a URL to the corresponding filesystem item.

So, basically, what you want is something like this:


function MapPath ($URL) {
if (strpos($URL,BASEURL) === FALSE) {
if (strpos($URL,"/") === 0) {
return (BASEDIR . $URL);
} else {
$dir = dirname($_SERVER['PHP_SELF']);
if ($dir != "/") $dir .= "/";
return (BASEDIR . $dir . $URL);
}
} else {
return (BASEDIR . substr($URL,strlen(BASEURL)));
}
}



BASEURL and BASEDIR should contain the base URL "http://www.site.net/dir" and "/home/name/public_html/dir". The parameter passed can be a relative or absolute URL (always inside your site, of course).

Anyway, that's only my solution. I've searched for such a funcion before and was very disappointed that PHP doesn't provide a quick and clean solution to this.

Please let me know if you find a better solution.

Hope it helps.

Share this post


Link to post
Share on other sites
The path to the root of your website is stored in the variable $_SERVER['DOCUMENT_ROOT'].

This means you can use

<?
fopen($_SERVER['DOCUMENT_ROOT'].'/shite/test.dat');
?>


to open the file from your example.

If you have all your files stored under one path you can define the basic path and make it work like this:

<?
define('FILEPATH', $_SERVER['DOCUMENT_ROOT'].'/shite/');
fopen(FILEPATH.'test.dat');
?>


Hope this helps.

Share this post


Link to post
Share on other sites
Thanks guys. I think I'll use a combination of your suggestions for now.


function MapPath( $path )
{
if( strpos( $path, "/" ) == 0 )
return $_SERVER['DOCUMENT_ROOT'] . $path;
else
{
$dir = dirname( $_SERVER['PHP_SELF'] );
if( $dir != "/" )
$dir .= "/";
return $_SERVER['DOCUMENT_ROOT'] . $dir . $path;
}
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!