How can I deal with the file which users upload? - database

If I have a website, and every user can upload his or her file, after I get this file, how can I deal with this file.
Should I store it to a specific folder(like upload), and store the file path in a specific database(for example table upload_file)?
Or should I just store this file in the database, if I can, how can I apply this to reality??
I think the first way is much better, but can I store all upload files in the same folder, or should I make a folder for each user?
Maybe I can encrypt the file name with md5 or anothers?
I do not know in real website development, how can you deal with the file, and keep the relationship with the user?
Thanks for your help !!

If they are small, just store it in the database;
Otherwise store they in a floder

Related

Server backend: how to generate file paths for uploaded files?

I am trying to create a site where users can upload images, videos and other types of files.
I did some research and people seem to suggest that saving the files as BLOB in database is a Bad idea; instead, save the file paths in database.
My questions are, if I save the file paths in a database:
1. How do I generate the file names?
I thought about computing the MD5 value of the file name, but what if two files have the same name? Adding the username and time-stamp etc. to file name? Does it even make sense?
2. What is the best directory structure?
If a user uploads images at 12/17/2013, 12/18/2018, can I just put it in user_ABC/images/, then create time-stamped sub-directories 20131217, 20131218 etc. ? What is the best structure for all these stuff?
3. How do all these come together?
It seems like maintaining this system is such a pain, because the file system manipulation scripts are tightly coupled with the database operations(may also need the worry about database transactions? Say in one transaction I updated the database but failed to modify the file system so I need to roll back my database?).
And I think this system doesn't scale (what if my machine runs out of hard disk so I need to upload the files to a second machine? What if my contents are on a cluster?)
I think my real question is:
4. Is there any existing framework/design pattern/db that handles this problem?
What is the standard way of handling this kind of problems?
Thanks in advance for your answers.
I've actually asked this same question when I was designing a social website for food chefs. I decided to store the url of the image in a MySQL database along with recipe. If you plan on storing multiple images for one recipe, in my example, maybe having a comma separated value would work. When the recipe loaded on the page, I would fetch the image associated with that recipe onto the screen.
Since it was a hackathon and wasn't meant for production purposes, I didn't encode the file name into something unique. However, if I were developing for productional purposes, I would append the time-stamp to the media file name when storing it into the server and database/backend.
I believe what I've proposed is the best data structure of handling this scenario. Storing the image onto the server is not only faster, but it should also take less space. I have found that when converting a standard jpg file of reasonable resolution to base64 encoding, the encoded text file representation took 30% more space. There is also the time of encoding the file and decoding the file for storage and resolving when using some BLOB type of data format instead of straight up storing the file on the server.
Using some sort of backend server scripting like PHP, you'll be able to do some pretty neat stuff with the information you have available. Fetch the result from the database, and load it in from the page using HTML.
As far as I know, there isn't a standard way of fetching media from a database yet. Perhaps there will be one day.
There is not standard way to do that, it is different to the different application. The idea is you need generate a different Path+FileName for every upload, here is a way:
HashId = sha1(microsecond + random(1,1000000));
Path = /[user_id]/[HashId{0,2}]/[HashId{-2}];
FileName = HashId

Best file permission for users to upload and delete files and folders

Lets say I let people upload files on the server. Should I change the file name s
after the upload ? if yes, how can I tell the about the file names later on , beacause in CMS I need tpo provide them with the url to images or doc files etc so they refer to them while producing text for the website.
I coded a program with which my users can navigate through some files and folders
they can create folders and upload files of any extension inside the mentioned folders
. Unfortinately my code creates all files and foldes with 777 permission. I wanna know
What would happen if someone uploads a .php file inside a folder and runs it. Can he/she
Delete every possible thing ? May you help me with right permission I should give to those files
and folders. The uploader should know about the url where he/she has uploaded
the image so while creating some content he can refer to them as url for images or
office word documents. Please help me with security hole.
Thank you.
I suggest you create some database table as a reference to those file so that you can record the old name, and changing the name in file system to different one so that you will never overwrite an existing file with same name.Or you can even same file in DB instead of file system.
First thing is you should not allow user upload any executable files to your server, or at least you need to change the extension name to something else I think. And I don't think 777 is needed, you just need to make sure the folder r/w to the php/web server processor and that's should be enough for any user access them

Basic File storage

In order to prevent file storage problems like when two people upload a file that might have the same file name...
Is it better to get each user a separate folder to prevent issues or is better to have all files in one folder for all users but change the file-name to keep them unique?
It depends on what you are trying to achieve.
What kind of service do you want to provide? A general file storage service? Then use different folders, since the number of files in a directory may be limited (depending on the file system) and can have major influence on the performance.
Do you provide an upload area for a simple blog? Use a single directory and change the file names.
Sorry, an absolute answer can only be given if you provide more information.

grails file upload

Hey. I need to upload some files (images/pdf/pp) to my SQLS Database and thereafter, download it again. I'm not sure what is the best solution - store it as bytes, or store it as file (not sure if possible). I need later to databind multiple domain classes together with that file upload.
Any help would be very much apreciated,
JM
saving files in the file system or in the DB is a general question which is asked here several times.
check this: Store images(jpg,gif,png) in filesystem or DB?
I recommend to save the files in the file system and just save the path in the DB.
(if you want to work with google app-engine though you have to save the file as byte array in the DB as saving files in the file system is not possible with google app-engine)
To upload file with grails check this: http://www.grails.org/Controllers+-+File+Uploads

Best methodology for file uploading (how does YouTube do it?)

What is the best method to follow for uploading files to server using my website? Each user will upload some files to his profile. Should I place all files in a single directory or do I need to create folders for each user and keep the files there?
How does YouTube do this? How should I store the uploaded info in database? What's the most efficient approach to do this when handling large number of users and files? I don't want to know about the usage of API. I want to know the best approach for file organization.
I would have a folder for each user, its more managable!
I wouldn't fancy a massive folder with loads of files!
You could end up with duplicate file names and end up deleting another users files!
I would create a directory for every user!

Resources