Skip to main content
gamedev.net
🔒 Locked

[web] apache+php+paths

Started by Lantz Oct 14, 2004 at 8:13 AM 2 replies 1.4k views
Original Post
Lantz
Lantz
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
jjmontes
jjmontes
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.
-=[ J ]=-I always forget to change the tagline.
Gom Jabbar
Gom Jabbar
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.
Gom Jabbar
Lantz
Lantz
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;	   	}	}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.