<p>So, you're writing a web application and you have several areas of the site where the user can upload files. My basic working method for this is to store the actual file on the server, and have a database table that connects the stored filename to the record it relates to.</p>

<p>My question is this: Should there be a different table for each "type" of file? Also, should the files be stored in context-related locations on the server, or all together?</p>

<p>Some examples: user profile photos, job application CVs, related documents on CMS pages, etc.</p>

[{'answer_id': 17724, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 2, 'selected': False, 'text': "<p>From what you've said I would just store files with random (UUID or what-not) filenames in one place. I would then have a 'attachments' table or something that contains references to all your external files. This table would also contain the meta-data for that file, so what type of file it is (picture, CV etc) and so on.</p>\n\n<p>There may be hard limits to the number of files in one directory though, depending on what FS you are using.</p>\n"}, {'answer_id': 17726, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 2, 'selected': False, 'text': "<p>There might be various reasons for storing different files in different locations.</p>\n\n<p>Firstly, a restriction on the number of files in one directory might be a consideration.</p>\n\n<p>Secondly security might be an issue - if some are to be publicly viewable (such as profile photos for example) but others are not (such as CVs) then placing them in different directories would be easier to manage.</p>\n\n<p>Thirdly, simple admin tasks may be easier if files are split, browsing in a file explorer for example, or managing backups, or modifying the application to split file storage across multiple locations.</p>\n\n<p>There is also the issue of filename conflicts, but if you rename everything to match the database id field (for example) then this wouldn't be an issue.</p>\n\n<p>But at the end of the day it probably depends on volumes and your own preference.</p>\n"}, {'answer_id': 17728, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>A different table for each file type only becomes relevant if you store other metadata (and therefore, additional columns) for each type of file. If your tables for each file type only contain the same columns (e.g., filename, filetype, dateuploaded, etc) then it would make sense to have them all on one table.</p>\n'}, {'answer_id': 17730, 'author': 'Cheekysoft', 'author_id': 1820, 'author_profile': 'https://Stackoverflow.com/users/1820', 'pm_score': 3, 'selected': True, 'text': '<p>From your example, there is an argument for two tables, as you have files that can be associated with two different things.</p>\n\n<ul>\n<li>CVs, photos are associated with a user.</li>\n<li>attachments are associated with a CMS page.</li>\n</ul>\n\n<p>If you put these in one table, (and you want to allow users to have more than one photo or cv) then you need two link-tables to associate files->users and files->cms_pages. Arguably this implies a HABTM relationship, which is not correct and allows for inconsistent data. </p>\n\n<p>The two table approach is slightly cleaner and only allows files to be associated with the correct type of entity with a simple belongsTo relationship.</p>\n\n<p>But I don\'t think there is any "right" answer to this question, unless you need to store different types of metadata for different filetypes. </p>\n\n<p>Also be sure to store, or be able to calculate, the mimetype for each file so it can be served correctly back to the browser, with the correct HTTP headers.</p>\n'}]
['https://Stackoverflow.com/questions/17715', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1951/']