Current location: Hot Scripts Forums » Programming Languages » PHP » Help with absolute path and relative path


Help with absolute path and relative path

Reply
  #1 (permalink)  
Old 05-16-07, 10:25 AM
samot samot is offline
Newbie Coder
 
Join Date: Apr 2007
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Help with absolute path and relative path

I have a question concerning absolute path and relative path stored into db.
I stored the path of the uploaded images into the db using 2 options and having hard time displaying the images.

Option1: The paths of the uploaded images are stored using absolute path like the following:

PHP Code:

C:/Inetpub/wwwroot/default/album/images
Option2: The paths of the uploaded images are stored using relative path like the following:
PHP Code:

 /album/images

When I query my db to get $row[‘path’] and $row[‘imgfile’], so I can use it with the image to display the image, but it doesn’t show any image. But when I checked the property of the image icon, it only says http://localhost/

PHP Code:

$display_image <img src=\”” $row[‘path’] . $row[‘imgfile’] . >


When I used option 2 and it works.
PHP Code:

$display_image <img src=\”” $row[‘path’] . $row[‘imgfile’] . >

Also, when I used it like this is also work without query from db to get the path. The path is set in a config file.
PHP Code:

$path “C:/Inetpub/wwwroot/default/album/images/
$display_image 
<img src=\”” $path $row[‘imgfile’] . >

Can someone help me understand why it is happening like this?
Thanks.
Samot
Reply With Quote
  #2 (permalink)  
Old 05-16-07, 09:19 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Try this
PHP Code:

$root $_SERVER['DOCUMENT_ROOT'];

$path $root.'\images\picture.png'
That will get you the file SERVER SIDE.... a client will be accessing the picture via http so they will get to that picture by this path...

http://www.yoursite.com/images/picture.png

So if you if you want to display that picture through a web browser you need a path like above. Store it in the database like so...

"http://www.yoursite.com/images/picture.png" or
If the page is relative to the image directory like
"images/picture.png"

That help?
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #3 (permalink)  
Old 05-17-07, 05:28 AM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
Nova912 explained it nicely. I want to add that in my opinion it's always best to use relative paths instead of absolute ones. Imagine you have your website running all fine. And you want to create some new functionality. For that purpose, you create a subdomain "dev.yourdomain.com". Then your site won't really work well when you used only absolute paths.

Well, it will work, but with data from your live site, and that's not good.

Just my 2 cents
__________________
Jack Bauer makes Chuck Norris cry
Reply With Quote
  #4 (permalink)  
Old 05-17-07, 08:47 AM
samot samot is offline
Newbie Coder
 
Join Date: Apr 2007
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks you so much. I'll give it a try...
Reply With Quote
  #5 (permalink)  
Old 11-13-08, 10:12 PM
JahDave JahDave is offline
New Member
 
Join Date: Nov 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks nova912, I was making the mistake of forgetting the difference between Client-side and Server-side access.
Vicious - the reason you can't rely on relative paths is because they do not always work on the Server-side. If in PHP you include (require) a file from some other directory, and that file tries to include another PHP file, it is not defined whether the path is relative to the first file or the second. If the include tree is even deeper, the path could also be relative to the parent or the first (top) file. I have already seen a difference between XAMPP on my local machine and one of the service providers I use.
I do not know if similar problems crop up on the Client-side (I haven't really thought about this or tested it). I do know that I have wasted way too much time on this whole issue. nova912's reply helped a lot, but I have to consider this issue unresolved. A database lookup doesn't sound great though as I have multiple directories for multiple websites on multiple web hosts using PHP, PHP generated switchable CSS, iframes switching between local and remote sites...
Right now I am using a hard-coding solution because I have deadlines to meet, but I would be grateful for a simple, short, general solution.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
relative addressing to an access db? benq ASP.NET 1 06-08-06 04:49 PM


All times are GMT -5. The time now is 06:02 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.