Jump to content

Using absolute pathing for website images.


UrbanDweller

Recommended Posts

Hey,

Im wondering how I go about creating a directory path to an image folder that can be opened from any folder inside my root web directory www/WEBROOT/IMAGES/SHOP/image.jpg

 

When i add an absolute path to the images url in the database when its gets executed it will run the the path from the current folder. I was wondering how to make its always start looking for files from the root directory down instead of the current location. The reason im asking this is that i was to be able to open those images from seperate locations inside my website.

 

Thanks

Link to comment
Share on other sites

Would $_SERVER['DOCUMENT_ROOT'] satisfy your needs? If necessary, define it in your initial index.php, and use it in your application from thereon out.

The document root directory under which the current script is executing' date=' as defined in the server's configuration file. [/quote']
Link to comment
Share on other sites

Would $_SERVER['DOCUMENT_ROOT'] satisfy your needs? If necessary, define it in your initial index.php, and use it in your application from thereon out.

The document root directory under which the current script is executing' date=' as defined in the server's configuration file. [/quote']

 

I tried that before, I had it defined in a config file included in all the nessacary pages but it didnt seem to pass very well when i tried to join $_SERVER["DOCUMENT_ROOT"] with a image location variable but didn't work.

 

Could you give me an example of root_document and if you can join a defined and a variable cause watever I tried doesnt work

 

define("SRV_ROOT", $_SERVER["DOCUMENT_ROOT"])
$imgDir = SRV_ROOT . "images/shop/img_cat";
// THIS FAILS

Link to comment
Share on other sites

Would $_SERVER['DOCUMENT_ROOT'] satisfy your needs? If necessary, define it in your initial index.php, and use it in your application from thereon out.

The document root directory under which the current script is executing' date=' as defined in the server's configuration file. [/quote']

 

I tried that before, I had it defined in a config file included in all the nessacary pages but it didnt seem to pass very well when i tried to join $_SERVER["DOCUMENT_ROOT"] with a image location variable but didn't work.

 

Could you give me an example of root_document and if you can join a defined and a variable cause watever I tried doesnt work

 

define("SRV_ROOT", $_SERVER["DOCUMENT_ROOT"])
$imgDir = SRV_ROOT . "images/shop/img_cat";
// THIS FAILS

 

First, make sure that you are calling $_SERVER['DOCUMENT_ROOT'] (and setting whatever variable) in the index.php located in your root directory.

Second, the variable does not, at least not with me, have an ending slash. So try SRV_ROOT . "/images/shop/img_cat" instead.

Finally, if it still is having issues - debug! A simple die($imgDir); is sufficient to find out what the variable actually holds. Then you can compare it to the actual correct path, and see exactly where the issue is.

Link to comment
Share on other sites

Thanks for that I now finally understand what this code i was using as a reference means :P

$thisFile = str_replace('\', '/', __FILE__);
$docRoot = $_SERVER['DOCUMENT_ROOT'];

$webRoot  = str_replace(array($docRoot, 'library/config.php'), '', $thisFile);
$srvRoot  = str_replace('library/config.php', '', $thisFile);

define('WEB_ROOT', $webRoot);
define('SRV_ROOT', $srvRoot); 

 

That die() command will come in handy didnt really know how to use it cept for sql examples :S

 

I just tried the die it returned "website/images/shop/img_cat/image.jpg" yet the rest of the script doesnt work until i create a local variable with the same path :S whats wrong

Link to comment
Share on other sites

Ok, well i got it to work all fine from both full comp path and web path but when I try open the image via the database url the src="c:/wamp/www/website/images/shop/img_cat/image.jpg"

 

if i try open the image url directly i get current path and image path conjoining

 

You don't have permission to access /website/admin/catagory/C:/wamp/www/website/images/shop/cat_img/a7aabac143.jpg on this server.

 

This has happened in the past but didnt think to check the link directly. How to stop this happening?

Link to comment
Share on other sites

Ok, well i got it to work all fine from both full comp path and web path but when I try open the image via the database url the src="c:/wamp/www/website/images/shop/img_cat/image.jpg"

 

if i try open the image url directly i get current path and image path conjoining

 

You don't have permission to access /website/admin/catagory/C:/wamp/www/website/images/shop/cat_img/a7aabac143.jpg on this server.

 

This has happened in the past but didnt think to check the link directly. How to stop this happening?

 

Hmm, your srvRoot and webRoot variables point to specific files. I don't know how you use these in your application, but I implement web/server root variables by first calling the root variable, which is generally the absolute path, ex. C:\webserver\www, and then put the relative name of the file after that.

So, an example implementation (that works):

 

/index.php

define('SERVER_ROOT', $_SERVER['DOCUMENT_ROOT']); //ex. C:/webserver/www
define('URL_ROOT', 'http://example.com/');
// other code here...

 

/files/stuff.php

$relativePath = '/my/directory/path/';
$fileName = 'theFile.png';
$path = SERVER_ROOT . $relativePath . $fileName; // C:/webserver/www/my/directory/path/theFile.png
$url = URL_ROOT . $relativePath . $fileName; // http://example.com/my/directory/path/theFile.png

 

I hope this makes it more apparent as to how this all fits together.

Link to comment
Share on other sites

Thanks I understand the defining path stuff enough to get it working and inserting the absolute path into sql database, but like i said when i call it from the database and put it inside the html img src tag it adds the current web path to the image url from the database creating a useless url.

 

It seems that everything requires a relative path in html/php cause once i put that url inside <img src"url" /> it seems to start looking from its current location not from where i ask in srv_root.

 

I really cant imagine why this is so hard to define between a bloody relative and absolute path without adding its own path. Is this HTML or PHP issue, do i have to enable absolute pathing. IM CRAZY ATM!!

 

Link to comment
Share on other sites

it adds the current web path to the image url from the database creating a useless url.

 

There is no such thing as a *web path*.

 

Can we see some examples of the paths you are storing? How your using them in your markup? And the absolute path to your images?

Link to comment
Share on other sites

Images on web pages are requested by the browser using a URL, i.e. <img src="URL_of_the_image" alt="">

 

If you use a leading slash / on a URL, it forms a domain relative URL. When the browser finds a leading / in a URL that is on a web page, it takes the http://domain.com portion of the current page and appends the /some_path/some_file.ext to form the actual URL that it will request.

 

Things like the following are file system paths on the server and don't have anything to do with urls or images on web pages -

 

www/WEBROOT

$_SERVER['DOCUMENT_ROOT']

include_path

c:/wamp/www/website

 

Link to comment
Share on other sites

If you use a leading slash / on a URL, it forms a domain relative URL. When the browser finds a leading / in a URL that is on a web page, it takes the http://domain.com portion of the current page and appends the /some_path/some_file.ext to form the actual URL that it will request.

 

Thanks man that did it, but now the below script doesnt work, the variable i create with the url inside doesnt pass into the previous code correctly. Is it due the variable starting with a forward slash?

 

if(move_uploaded_file($_FILES['catImage']['tmp_name'], $imgDestination)){
		 return($imgDestination);
	 }

Link to comment
Share on other sites

Everything I posted above concerns <img > tags on web pages and forming URLs that the browser uses to fetch the image in order to display it.

 

move_uploaded_file operates on files on the server, using file system paths, not URLs. A leading / on a file system path refers to the root of the current hard disk. To form an absolute file system path, starting at your document root folder, you would use $_SERVER['DOCUMENT_ROOT'] as the starting point, then append (concatenate) the path/filename.ext to get the absolute path to the file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.