rocket_roche Posted January 12, 2009 Share Posted January 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2009 Share Posted January 12, 2009 Your query should look like.... INSERT INTO booktable (title, path) VALUES ("booktitle","/images/imagename.jpg") Then, to display the image would be something like.... echo "<img src='{$row['bookcover']}'>"; Quote Link to comment Share on other sites More sharing options...
ILMV Posted January 12, 2009 Share Posted January 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
trq Posted January 12, 2009 Share Posted January 12, 2009 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 12, 2009 Share Posted January 12, 2009 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) Quote Link to comment Share on other sites More sharing options...
rocket_roche Posted January 12, 2009 Author Share Posted January 12, 2009 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?? Quote Link to comment Share on other sites More sharing options...
Mchl Posted January 12, 2009 Share Posted January 12, 2009 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. Quote Link to comment Share on other sites More sharing options...
nadeemshafi9 Posted January 12, 2009 Share Posted January 12, 2009 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. Quote Link to comment 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.