ashok
ashok

Reputation: 85

what to store image or its path in mysql database

I am using MySQL and i want to store some images. I read on forum that instead of storing directly images as 'BLOB', store images in directory and there path in database. But what if user will delete any image from directory(or by some other means image gets deleted). Is there any way to handle that situation ? And why its not preferd to store images directly in db?

EDIT: The database is not related to website development. I am developing a software in which user will take some images using camera and after playing with it, I want to store a record for that image in database with some other parameters(in which one column is for storing image). The software and database is going to run on users PC. And I am going to generate (probably in excel) final report of all records.

Upvotes: 0

Views: 1131

Answers (2)

Alexander Ejbekov
Alexander Ejbekov

Reputation: 5960

You can never be 100% sure that the images won't get deleted. Of course for extra security you can give permission only to a certain user to the directory and the files inside. Downside of storing blobs - as much as MySQL is capable of handling quite big databases, images can add up ridiculous amount of size to the database and it will be much harder to manage, maintain and optimize if it gets really big. Migration will be really difficult if you decide to swap the engine, and even more painful if you decide to go with a noSQL solution(couchdb, mongo, etc.). Perhaps you could set up a cron to iterate through all the records and check if the file exists. If it doesn't, drop the record from the table. Then again I'm not sure what you are trying to do exactly. If a user has the option to delete an image, I assume it's going to happen through some interface. You will need to implement a logic so that:

1) The file gets deleted

2) The record is deleted from the database.

Upvotes: 0

Akhil Sidharth
Akhil Sidharth

Reputation: 746

Well, while storing image in mysql can lead to increasing size and load for the database. If your site have a high volume, traffic site, this is not a good idea. Instead, i usually prefer to store the image files in a folder & update its path in the database. The main thing you need to take care is setting a unique name (such as img1_2014_04_20_16_30_25.JPG) in order to prevent the file getting overwritten. In the above mentioned name can be made unique using the time functions easily. Then there is no direct way of handling such error with mysql as far as i know, all you can do with your front end is to make sure the image exist or put a default no image as display.

Upvotes: 1

Related Questions