determine where user is

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

determine where user is

Post 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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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']);
ml01172
Forum Newbie
Posts: 4
Joined: Mon Dec 11, 2006 3:55 am

Post 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";
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:arrow: Moved to PHP Code
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply