I am building a web application and users will be able to upload pics. And I want to store these images into files instead of storing them into the DB.

But if I saved these pics into relative paths to the web application, they will be removed with each build.

So how I can handle this and the best practices related to this issue.

  • Where CAN you put the files?
    – user1249
    Oct 3 '11 at 12:16
  • 1
    You are letting users upload files into a path served up by the web application? Don't worry about overwriting your scripts. Hackers will do that for you.
    – JohnFx
    Oct 3 '11 at 19:38

Well, you'll need to store a reference to them within a database, then store the images external to the web application. It could be on the same server in an accessible folder outside of the scope, or on a dedicated server for hosting the images.


For simplicity of design and code it sometimes makes sense to store files as binary data in a database column, however RDBMS isn't efficient for this and I wouldn't recommend this if performance is a primary concern.

If you decide to store on filesystem then absolute paths are much better than relative paths as you have persistence. Designate an absolute path to a folder to store files and make sure it is regularly backed up.

Another note of advice, when it comes to hosting, it is best that you NOT store files on the web server itself. Most web server machines are configured to do one thing and one thing, receive and serve network communication. Compared to a sophisticated file storage, backup, or other RAID array system the disk storage on most web servers is comparitively underpowered for large amounts of file I/O. Webservers are just not intended for this purpose.

I recommend an absolute path to a network share to a dedicated file server or other system intended for large amounts of file I/O as well as backups and redundancy.

  • But the absolute paths for each development phase is probably different.
    – NoChance
    Oct 3 '11 at 13:21
  • @EmmadKareem - Absolute path goes in your web.config so you can change it in each environment. (+1 for sensible approach)
    – Joel Brown
    Mar 15 '12 at 13:37

Good way to deal with this kind of issue is to have another domain/subdomain/(or even CDN service) that is dedicated to serving static files (images, scripts, stylesheets, documents, etc).

So for example in simple web applications: Application path: /www/app/, Static files path: /www/static/

when user uploads a file, saving it to /www/static/complex/folder/structure/file.ext, and then save details about this uploaded file to your database. and then serving the file trought static.my-amazing-web.app

What this will enable you to do is: easily leverage the users browser cache, do optimizations on the fly, scale out with CDN's when things start to get out of hand, and all kinds of fun stuff.

This will also enable you to implement more security for the files the users upload. It is also good idea to have static file server that has execution of the files blocked, to improve the security of the server from malicious user uploads.


Do both -- store them in a database initially. Cache them onto disk when requested. You get the best of both worlds -- easy transportable backup with scaleable distribution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.