Page 1 of 1

determine where user is

Posted: Sat Nov 25, 2006 8:42 pm
by speedy33417
I'm about to get started on a quite large site, where there will be several subfolders used. I'm going to have one images folder, but will need to access it from all around the site.
Because changes are expected I would like to create a function to generate a path to the images folder no matter which subfolder the user's at.

The idea is to submit the file name and receive it with a full path.

$fileName = "logo.jpg";

//after function $fileName returns as:

"../images/logo.jpg"
or
"../../images/logo.jpg"
or simply
"images/logo.jpg"

Thanks.

Posted: Sat Nov 25, 2006 9:10 pm
by Zoxive
Why not just use absolute urls?

Code: Select all

http://www.test.com/images/image.jpg
or

Code: Select all

/www/public_html/images/image.jpg
etc..

or if you really need to... maybe something like

Code: Select all

$url = explode("/",$_SERVER['REQUEST_URI']);

Posted: Wed Dec 13, 2006 5:05 am
by ml01172
Why not make a file images_location.php, with the following contents:

// file: images_location.php
if ( ! defined( IMAGES_LOCATION_PHP ) ) {
define( "IMAGES_LOCATION_PHP", true );
define( "IMAGES_LOCATION", "/home/user/public_html/site/images/" );
}

Then from your PHP file you use the following:

require "images_location.php";
$image_name = "slika.jpg";
$image_path = IMAGES_LOCATION . $image_name;
echo "<img src=\"$image_path\" alt=\"Pozdrav\" />\n";

Posted: Wed Dec 13, 2006 5:35 am
by CoderGoblin
There has been debate on wether or not to use constants before in these forums but I have to admit that IMAGE_PATH is one constant I frequently use. Hard coding the url path is a pain if it ever needs to change. Far better to be able to just change the definition is one place.

Posted: Wed Dec 13, 2006 8:00 am
by Chris Corbyn
:arrow: Moved to PHP Code

Posted: Wed Dec 13, 2006 8:07 am
by feyd
Burrito posted geturl() which may be useful.

If you wish to use relative references then you likely will need to determine the full URL of the image or other entity then compare the resulting strings, remove the common directories, then determine how many (if any) directories backward the reference is. (this can be accomplished via count() and dirname() and/or explode()) and some concatenation, possibly str_repeat() too.