0

I was wondering if there are other ways than the way I want to retrieve a image from my database. When I use this code I get a broken image icon and that's it! The way it works is that I store the location path of an image in the database like such:

$target=$_SERVER['DOCUMENT_ROOT'] . "/images/" . basename( $_FILES['file']['name']);

and then retrieve it by php lke such:

echo '<img src="'. $_SESSION['pic_location'] .'"/>';

Now in my database it gives the location path like such for e.g.

C:/xampp/htdocs/images/beetgejo.jpg

I don't think it will work when the ENTIRE path is stored?? Can it be stored like such:

../images/beetgejo.jpg
Joe_B
  • 219
  • 3
  • 10
  • 19

3 Answers3

0

Yes, it posible to use

../images/beetgejo.jpg 

its called relative path, its relative to your current location, an i sugest using this as its more flexible. Path returned from database is wrong, try saving relative

for ex: save only file name to database, image.jpg and include it later in image tag, by defining relative path.

<img src="/images/file_name_from_db.jpg" />
Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • Can I maybe change the path in the code instead of using $_SERVER['DOCUMENT_ROOT'] to something like ../images/beetgejo.jpg? Because then it works but NOT with the FULL path like C:/xampp/htdocs/images/beetgejo.jpg – Joe_B Oct 16 '11 at 21:10
0

It is called relative path. You can simply store the full URL for the image in the database using $_SERVER variable.

'http://'.$_SERVER["SERVER_NAME"].'/images/'.basename( $_FILES['file']['name']);

Now you can get relative URL from current URL using functions like this : Getting Relative Path From Absolute Path

Community
  • 1
  • 1
Sgn.
  • 1,696
  • 2
  • 14
  • 27
0

You're storing the path in your database. This is OK as far as it goes, but you have the potential for that data to become stale -- for the file to be moved, but the database never informed, with the result that your database data is inaccurate. Since it sounds like you're interested in more than simply storing URL paths...

Personally, I'd prefer to store the image data inside the database if possible. Here is my preffered reference regarding that practice. It won't form a complete picture all by itself, but it's a great starting point and you can branch out from there. You can either use a simple php file to act as an image retriever (browsers have no trouble trying to access a php file to retrieve an image, so long as the header on the return message is set properly), or you can even use URL rewriting to hide the fact that you're using a database.

RonLugge
  • 5,086
  • 5
  • 33
  • 61