1

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.

Abdullah Shoaib
  • 416
  • 1
  • 5
  • 18
  • 1
    Check some Intermediate solutions, pros/cons of saving the full or incomplete path here: https://stackoverflow.com/questions/4444442/how-do-i-store-the-location-of-an-image-in-a-database/67593646#67593646 – KushalSeth May 18 '21 at 20:42

4 Answers4

7

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.

Igor Skobelev
  • 339
  • 1
  • 6
  • I have the same opinion as yours, but why wordpress store full image path in database? Is wordpress developer community not following best practices ? – Abdullah Shoaib Aug 30 '18 at 12:03
  • 1
    I am sorry, I am not expert in Wordpress but in general both of this cases can be used, even if you store full path and need to move all your images to another server you can write DB migration SQL like update set img = replace(img, 'www.abc.com', 'www.def.com'); But it more useful to skip it.
    – Igor Skobelev Aug 30 '18 at 12:12
  • I found a great tool for replacing old domain with new domain for wordpress database. https://rudrastyh.com/tools/sql-queries-generator – Abdullah Shoaib Aug 30 '18 at 12:19
  • 1
    Yes, as you can see it is set of same query from comment above: update set img = replace(img, 'www.abc.com', 'www.def.com') but for all tables, where wordpress store images. Also please check my another comment about polymorphic associations. Maybe this way is more suitable for you.
    – Igor Skobelev Aug 30 '18 at 12:30
1

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.

Martin
  • 2,326
  • 1
  • 12
  • 22
0

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" />

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • Is it good to save image URL with domain name ? e.g. abc.com/src/apple.png. If i change the domain name then images will not going to work. – Abdullah Shoaib Aug 30 '18 at 11:57
0

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.

Igor Skobelev
  • 339
  • 1
  • 6