UrbanDweller Posted April 1, 2012 Share Posted April 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/ Share on other sites More sharing options...
TimeBomb Posted April 1, 2012 Share Posted April 1, 2012 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'] Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333245 Share on other sites More sharing options...
cpd Posted April 1, 2012 Share Posted April 1, 2012 You can look into adjusting your ini files include_path option. http://uk3.php.net/manual/en/ini.core.php#ini.include-path Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333246 Share on other sites More sharing options...
UrbanDweller Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333249 Share on other sites More sharing options...
TimeBomb Posted April 1, 2012 Share Posted April 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333251 Share on other sites More sharing options...
UrbanDweller Posted April 1, 2012 Author Share Posted April 1, 2012 Thanks for that I now finally understand what this code i was using as a reference means $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 Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333252 Share on other sites More sharing options...
UrbanDweller Posted April 1, 2012 Author Share Posted April 1, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333253 Share on other sites More sharing options...
TimeBomb Posted April 1, 2012 Share Posted April 1, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333254 Share on other sites More sharing options...
UrbanDweller Posted April 1, 2012 Author Share Posted April 1, 2012 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!! Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333257 Share on other sites More sharing options...
trq Posted April 1, 2012 Share Posted April 1, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333260 Share on other sites More sharing options...
cpd Posted April 1, 2012 Share Posted April 1, 2012 I think Thorpe, by web path he means the http root. Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333265 Share on other sites More sharing options...
PFMaBiSmAd Posted April 1, 2012 Share Posted April 1, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333280 Share on other sites More sharing options...
UrbanDweller Posted April 2, 2012 Author Share Posted April 2, 2012 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); } Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333461 Share on other sites More sharing options...
PFMaBiSmAd Posted April 2, 2012 Share Posted April 2, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260127-using-absolute-pathing-for-website-images/#findComment-1333515 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.