Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a file just a file?

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.

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?

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

like image 461
mercutio Avatar asked Aug 20 '08 10:08

mercutio


People also ask

What does it mean when a file is just a file?

file file extension is a generic extension. This is assigned by Windows and other applications when the file is in an unknown format.

What are the three types of file?

The types of files recognized by the system are either regular, directory, or special. However, the operating system uses many variations of these basic types. All file types recognized by the system fall into one of these categories. However, the operating system uses many variations of these basic types.

How do I open a .file file?

How to open a FILE file. Before opening a FILE file, you should verify its contents. You can use file identification utilities like File Viewer Plus, TrIDNET, File Viewer for Mac, File Viewer for Android, or the Linux file command to identify a file's contents.


3 Answers

From your example, there is an argument for two tables, as you have files that can be associated with two different things.

  • CVs, photos are associated with a user.
  • attachments are associated with a CMS page.

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.

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.

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.

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.

like image 169
Cheekysoft Avatar answered Sep 21 '22 05:09

Cheekysoft


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.

There may be hard limits to the number of files in one directory though, depending on what FS you are using.

like image 25
SCdF Avatar answered Sep 20 '22 05:09

SCdF


There might be various reasons for storing different files in different locations.

Firstly, a restriction on the number of files in one directory might be a consideration.

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.

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.

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.

But at the end of the day it probably depends on volumes and your own preference.

like image 25
samjudson Avatar answered Sep 23 '22 05:09

samjudson