Saving images in mysql database

What is the best way to uniquely store and name an image in a database if two people have the same image name or the same user uploads the same image twice. How can I manage such scenarios so that when I want to perform operations such as updating, deleting, I do not delete both images or all. My DB is MySQL.

Edit - Okay, now for some reason I created a unique timestamp for the whole image, everything works on localhost, but when I take it on the Internet, it doesn’t work, I can’t delete it, but it works offline well.

Thank. @cybeormin

+1
php mysql
Nov 09 '10 at 12:20
source share
5 answers

In any table, I would recommend an identifier column that is auto-incremented and set to the main field. Thus, all lines are unique, despite the fact that the user has two images with the same name.

+1
Nov 09 '10 at 12:24
source share

Put refcount on the images. If you want to delete the image, simply reduce it. When refcount reaches 0, you delete the line for the real one (or do the cron job).

0
Nov 09 '10 at 12:24
source share

I would reset the original image name, give each image a unique identifier and save the original name in db.

0
Nov 09 '10 at 12:24
source share

In your mySQL database, create a table for loading images, create an identifier column set to AUTO_INCREMENT , INT (integer) and make it PKEY (primary key).

This means that your identifier is always automatically created and generated when new content is added and is always unique regardless of whether the same image is added as many times as necessary.

Then additional fields can capture other information about the image (file name, "given" name, file size, file type and BLOB field for the image itself), you can also add the psuedo identifier, which you compose based on any combination of factors ... you consider it necessary.

0
Nov 09 '10 at 12:27
source share

Associate a unique key (primary key) with each record. Something like:

 fileId | file_name | file 

and set fileId to auto incrementing and primary key. Then you can just refer to fileId as a foreign key in your "user" table (if you have something like that).

 userId | ... | fileId | ... 

Then, if you need to delete the file, simply use fileId to find the one you want to delete.

0
Nov 09 '10 at 12:28
source share



All Articles