dakke Posted February 8, 2008 Share Posted February 8, 2008 What is the 'best' solution for storing and displaying an image from a MySQL db?I used blob so far, but don't know whether it's the best and fastest solution really...Any suggestions? Link to comment Share on other sites More sharing options...
teng84 Posted February 8, 2008 Share Posted February 8, 2008 it not right to store images in your database.. I suggest instead of inserting images in your db just store the location of each images.. so you only need to call those images. Link to comment Share on other sites More sharing options...
jhecht Posted February 8, 2008 Share Posted February 8, 2008 it not right to store images in your database.. I suggest instead of inserting images in your db just store the location of each images.. so you only need to call those images. ^^^^^ listen to him Link to comment Share on other sites More sharing options...
dakke Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks for that. I wrote the script to indeed save the path. But than it comes down to prevent overwriting files, no?Anyway, suggestions are welcome, but I'll trust you on the blob. Link to comment Share on other sites More sharing options...
jhecht Posted February 8, 2008 Share Posted February 8, 2008 thats why you rename the file when you upload it(usually using uniq_id(),time() and some hashing function). And if a user uploads files, then you could just create a directory based on their username(which would be unique) and just upload photos from there, checking to see if a the file exists already with the same name, if so, tell the user they must rename the file and then use move_uploaded_file etc etc. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 8, 2008 Share Posted February 8, 2008 It's not necessarily wrong to store files in a database, if that's what the application requires. If you want to store binary data a blob is the best type to use. Link to comment Share on other sites More sharing options...
deved Posted February 20, 2008 Share Posted February 20, 2008 It's not necessarily wrong to store files in a database, if that's what the application requires. If you want to store binary data a blob is the best type to use.An application that needs to match text in one column to the image in another column, for instance? If so, is that what a 'blob' is for? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 20, 2008 Share Posted February 20, 2008 Blob is for any binary data, it stands for binary large object. But in most cases it is a better solution to store the file on disk and store the filename or path to the file in the database, you incur a performance penalty when you need to get the binary data from the database and output it. Link to comment Share on other sites More sharing options...
jhecht Posted February 21, 2008 Share Posted February 21, 2008 That, and it makes it ###### for the poor guy who has to look at the code after you. I know if i saw a bunch of BLOB column types with image data i'd probably quit and tell them to get a different person. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 21, 2008 Share Posted February 21, 2008 It's really not that bad. If the data is messed up there's nothing you can do about it, but assuming they put it in the database correctly it's as easy as using file_get_contents to store something and it's a small script that gets it back out of the database and recreates the file. e.g.: <?phpinclude 'db.php';$result = mysql_query('SELECT image FROM table WHERE id=' . intval($_GET['id']));$row = mysql_fetch_assoc($result);header('Content-type: image/jpeg');header('Content-disposition: attachment; filename=image.jpg');header('Content-length: ' . strlen($row['image']));echo $row['image'];?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.