Skip to main content
January 5, 2010
Answered

Storing File path - Best Practice

  • January 5, 2010
  • 1 reply
  • 354 views

I have a db that stores the path of images that are loaded into a page.  All the files are stored in the same folder.  My question:  Is it better to store the entire file path in my db or should I just store the file name and make a Constant within the webpage calling the picture?  I would think it's the second option as it is less data in the db but I just wanted to make sure.

Thanks!

This topic has been closed for replies.
Correct answer David_Powers

If the path is always the same, I would store just the filenames. Another option is to create a new field in your table for the path, and assign the current path as the default value. When inserting records, just add the file name; the path field will just take the default value.

In your SQL:

SELECT *, CONCAT(path, filename) AS image

FROM mytable

If you already have records stored in the table, you can update them very quickly with two SQL queries:

UPDATE mytable SET path = '/path/to/images/folder/'

UPDATE mytable SET filename = REPLACE(filename, 'path/to/images/folder/', '')

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
January 5, 2010

If the path is always the same, I would store just the filenames. Another option is to create a new field in your table for the path, and assign the current path as the default value. When inserting records, just add the file name; the path field will just take the default value.

In your SQL:

SELECT *, CONCAT(path, filename) AS image

FROM mytable

If you already have records stored in the table, you can update them very quickly with two SQL queries:

UPDATE mytable SET path = '/path/to/images/folder/'

UPDATE mytable SET filename = REPLACE(filename, 'path/to/images/folder/', '')