Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

When to save URL images in a database vs when the file-system is enough

Hi,

What is the best way to manage images in the following situation…

I have a real simple CMS-like- app where images will be uploaded and stored in either the default storage or the public directory (not sure what is the best location) and then accessed in any part of the authenticated areas of the app. For instance the user will have the option to insert any of the uploaded images from/to any of the pages by inserting the URL of the selected image (using an inline HTML editor ).

  1. Do I need to save the URLs of the images in a database or saving them in a directory and then just retrieving the file names directly from the directory would be enough?

  2. Can someone describe a situation when It’s recommended to use a database and when to use the file system directly?

Thanks a lot

0 likes
8 replies
vipin93's avatar

if u want to use your app as api also then url i think is best

1 like
martinbean's avatar
Level 80

@fsdolphin I save just the filenames of uploaded files in my database. That way, I’m not tied to one particular storage mechanism. I can also move images between folders and change it one place, instead of having to upload multiple rows across many tables in a database.

1 like
fsdolphin's avatar

@martinbean Is that generally the process you follow, regardless of what type of app you are building?

kobear's avatar

I have done what @martinbean has done, where just the filename of the image is in the database. But I took it further, where I also had the target filesystem as a column, as well as the reference key for that target filesystem.

So, for instance, if I start app development with local storage, with the intent of moving to Amazon S3 later, i have set 'filesystem' as local, then the 'reference' is the relative path to the image.

I have also written automated migrations where the files are moved from local to S3, and just update the 'filesystem' and 'reference' after the copy is complete. That way, the platform did not have an outage, and users never noticed.

1 like
martinbean's avatar

@fsdolphin Pretty much, and have done for years. Has worked well up to now. As I say, means I can store the file in any location and at any path, so long as the filename stays the same.

1 like
fsdolphin's avatar

@martinbean One last question, does it matter where do I put my images, in the storage folder or the public folder? Is there a common practice?

vipin93's avatar

u should use storage folder and make symbolic link to map storage folder to public, it will secure your storage

martinbean's avatar

@vipin93 How does doing a symbolic link to the storage folder make files in there any more “secure”?

@fsdolphin If storing files on the same filesystem as your application, then common practice is to save the original uploads in the storage, and then serve generated thumbnails from somewhere under the public directory.

1 like

Please or to participate in this conversation.