Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

3 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
    
But the absolute paths for each development phase is probably different. –  Emmad Kareem 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
    
@Joel Brown, thanks for the clarification. –  Emmad Kareem Mar 15 '12 at 15:21
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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