Abdullah Shoaib
Abdullah Shoaib

Reputation: 464

Which is the best practice either to save image name or full URL in database

Which is the better approach for storing image name in database? I have two choices first one is to store just image name e.g. apple.png and second choice is to store full image URL e.g. abc.com/src/apple.png.

Any help will be appreciated. Thanks.

Upvotes: 1

Views: 11680

Answers (4)

Igor Skobelev
Igor Skobelev

Reputation: 337

Another common way is to create image table with cols

  • id
  • type (enum or int)
  • name (file name) Define in your app (better in model) types like

USER_AVATAR = 1; PRODUCT_IMG = 2;

Define path map foreach image type like:

$paths = [ USER_AVATAR => '/var/www/project/web/images/users', ... ];

and use id's from this image table in another tables. It is called polymorphic association. It is most flexible way to store images.

Upvotes: 0

Martin
Martin

Reputation: 2424

The 2 answers already covered it pretty well. It is indeed best practice to save the directory path instead of saving the entire URL path. Some of the reasons were already covered, such as making it easy to move your folders to another server without having to make any changes whatsoever in your file logic.

What you could do, is also have everything in one directory, refer to that, and then just save the image name. However, I would not recommend that. The other structure simply makes it way easier to navigate and look through. Good file structure is something you'll thank yourself for later in case you ever have to go through things manually for one reason or another.

With that said, I'd like to add this trick into the mix:

$_SERVER['DOCUMENT_ROOT']. This always makes you start from the root directory as opposed to having to do tedious things, such as ../../ etc. It looks like a mess.

So in the end as an image path, you'd have something like:

<img src="<?php echo $_SERVER['DOCUMENT_ROOT'].'/'.$row['filePath']; ?>" >

$row['filePath'] being your stored filepath from the database.

Depending on how your file path is saved, you can lose the / in the image source link.

Upvotes: 1

Igor Skobelev
Igor Skobelev

Reputation: 337

Best practice is not save full path to image like abc.com/src/apple.png but saving specific domain path to image. Ex:

  • Users image : /user/{id}/avatar/img.png

  • Product image: /product/{id}/1.png

In this case you avoid sticking images to defined server, server path, url etc. For example, you will decide to move all your images to another server, in this case you don't need to change all records in DB.

Upvotes: 7

Saurabh Mistry
Saurabh Mistry

Reputation: 13679

first of all you need to upload all images in public folder of your project , so no need to save domain name

If you are storing all images in one directory , then there is no problem storing only imagename in database

you can easily access images like <img src="/foldername/imagename.jpg" />

but if in your project there are multiple directory like

  1. profile :to save user avatar image ,
  2. background : to save background images,

then it is better to save image with path in database like "/profile/avatar.jpg"

so you can access image like <img src="imagepathhere" />

Upvotes: 0

Related Questions