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.
determine where user is
Moderator: General Moderators
- speedy33417
- Forum Contributor
- Posts: 128
- Joined: Sun Jul 23, 2006 1:14 pm
Why not just use absolute urls?
or
etc..
or if you really need to... maybe something like
Code: Select all
http://www.test.com/images/image.jpgCode: Select all
/www/public_html/images/image.jpgor if you really need to... maybe something like
Code: Select all
$url = explode("/",$_SERVER['REQUEST_URI']);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";
// 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";
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.