Jump to content

[SOLVED] Storing image paths in PHP and mySQL


rocket_roche

Recommended Posts

Hi,

 

I'm doing a college project on setting up an online bookstore. I wish to display a book cover image on the browser. I believe that storing a path to the image in mySQL is good practice (doesn't impact on performance). My query is based on the path to store...

 

I am using the default WAMP www folder with a folder called images. When storing the path to the images in mySQL table should I do this:

 

insert into booktable
values("booktitle","c:\wamp\www\images\imagename") 

 

or this

 

insert into booktable
values("booktitle","\images\imagename") 

 

Could someone please explain which path to store? Correct example?

 

Secondly the single line of PHP to display the image.  e.g.

 

echo ("<img src = " . $row["bookcover"] . ">");

 

("bookcover" = column name in the table that stores the image)

 

Any help greatly appreciated...

 

Regards,

 

Tony

 

 

Link to comment
Share on other sites

Yes storing image locations are fine, but consider storing just the file name, and then construct the URL when you return the field.

 

For example...

 

<img src="./images/thumbnails/<?php echo($row['image_location']); ?>" />

 

That way if you want to move the place where your files are stored, then you can do without having to update loats of records.

 

ILMV

Link to comment
Share on other sites

That way if you want to move the place where your files are stored, then you can do without having to update loats of records.

 

But doing it that way you may need to edit loads of files instead of simply executing an sql query.

Link to comment
Share on other sites

Store relative paths. It will be easier to move your application to a different server (even if you;re not going to do this for this particular one, that's the general idea)

 

Even better, store just image names, and define a path to image directory somewhere else (a config file for example)

Link to comment
Share on other sites

Thanks guys, thats really helpful and has improved my understanding.

 

The path I should be using is from the www folder.

 

Please clarify:

 

If I store just image names, it is possible to set up a path variable (as a function - or similar?) in a file, which I could include in all HTML pages? If I later move my image folder or rename it - I can simply change the centrally stored path variable??

 

 

Link to comment
Share on other sites

If I store just image names, it is possible to set up a path variable (as a function - or similar?) in a file, which I could include in all HTML pages? If I later move my image folder or rename it - I can simply change the centrally stored path variable??

 

That's exactly what I am suggesting.

Link to comment
Share on other sites

you shouldent store the path i do this every day so trust me, you should generate a random number on each image when its uploaded and also append its title to the image name and the extension, you can then use this image name from anywhere in your site by appending the correct path after you retrieve the image name from the db, storing the whole path limits you plus it wont work on Linux if ur using c: and all that.

 

 

echo ("<img src = ../images/" . $row["bookcover"] . ">");

 

 

 

 

echo ("<img src = ../img/" . $row["bookcover"] . ">");

 

 

 

echo ("<img src = /thumb/small/" . $row["bookcover"] . ">");

 

infact you can save teh same image in different places using teh same name and only need to store the name once in teh record, these different images can be made different sizes using teh GD library and u only have one name for all of them the differenc eis there in different places.

 

 

here is how you get the name

 

if($_FILES['drawings_gif']['size'] > 0) {

$gif_name = $_FILES['drawings_gif']['name'];

$gif_fullstop = strrpos($gif_name, ".");

$ext = strtolower(substr($gif_name, $gif_fullstop));

$new_gif_name = strtolower($new_file_name.$ext);

}

 

 

you probably whant to php.net/ereg_replace all the spaces in teh name with and underscore and all the other non alpha chars and append the time to the image this means you never need to delte the image, just update the record with the new image with adifferent time and you can keep the old image.

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.