上传和存储图片的最佳方式是什么? [英] What is the best way to upload and store pictures on the site?

查看:252
本文介绍了上传和存储图片的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道大网站如何将图片保存在他们的服务器上。任何人都可以告诉我他们如何保存用户上传的图片?



我在想,也许他们只是保存文件(图片)在一些路径,只是保存在数据库中的路径是正确的?



但我想这样做。这是正确的吗?例如,一个名为 www.photos.com 的网站。当用户上传图片时,我将创建一个用户名的文件夹,并将这些图片保存在该文件夹中。



我相信我们可以使用 php文件概念。所以,当一个新的用户上传他的图片或文件,我想创建一个名称与他的名字的目录。

例如:如果用户名是john,我会创建一个目录这在photos.com www.photos.com/john/ 上,然后在上传图片时将所有图片保存到此目录。这是正确的方法来做到这一点?

我没有人在这里有良好的文件保存到服务器的知识,所以请让我知道如何做到这一点?我想以正确和安全的方式做到这一点。 所有大网站不将图片保存到数据库他们将它们存储在磁盘中。
他们在表格中保存对图片位置的引用。然后从那里链接。



为什么?性能。

从数据库中提取大量内容是一个巨大的性能瓶颈。而且数据库不能很好地水平扩展,所以这意味着更大的问题。所有大型网站都使用静态内容农场来处理静态内容,如图像。这是服务器谁不会不在乎你的身份。



他们如何保持图片真正的私人,你可能会问?他们没有。



图片的链接本身就是地址和密码。例如,以Facebook为例。如果我在我的帐户上存储私人照片,则不能打开它。但是,只要你有正确的地址,你可以。



这张图片是私人的注意文件名


这是非顺序的。获取图片的唯一方法是知道它的地址。

基于以前的用户照片,你不能猜测下一个。

它有一个巨大的熵,所以即使你开始随机的猜测,你会有大量的失败,如果你确实得到了一个图片,你将无法从那里认识到所有者的身份本身就是匿名保护。


编辑(为什么不应该将图像存储在用户名文件夹中:

编辑之后,即使你的逻辑(为每个用户创建一个文件夹)接收更多的数据,但是你的目标是把文件放在磁盘上,而不是放在数据库上。当你开始拥有许多用户和许多图片时,组织它会产生问题,想象一下你的服务器有1T的磁盘空间,并且让我们想象1T对于服务器可以处理的负载或多或少是准确的。



现在你有11个用户,假设他们开始upl同时上传,每个会上传超过100GB的文件。当他们达到91GB每个服务器已满,你必须开始存储在不同的服务器上的图像。如果遵循该用户/文件夹结构,则必须选择其中一个用户并将其所有数据迁移到不同的服务器。另外,对于不能在文件中上传超过1T的用户,这是一个硬性限制。

我应该将所有文件存储在同一个文件夹中,那么?
不,大网站通常将文件存储在顺序文件夹(/ 000001 /,/ 000002 /等)中,每个文件夹具有x个定义数量的文件。这主要是文件系统性能问题。



更多关于 目录中有多少文件太多?


I have no idea how the big websites save the pictures on their servers. Could any one tell me how do they save the pictures that are uploaded by the users in their database?

I was thinking, maybe they would just save the file(the picture) in some path and just save that path in the databse is that right?

But I want to do it this way. Is this right? For example, a website named www.photos.com. When a user uploads a picture I would create a folder of the user name and save those pictures in that folder.

I believe we can create a directory using php file concepts. So when a new user uploads his picture or file, I want to create a directory with his name.

Example: if user name is john, I would create a directory like this on photos.com www.photos.com/john/ and then save all his pictures to this directory when he uploads a picture. Is this the right way to do this?

I have no one here that has good knowledge of saving the files to servers so please let me know how to do this? I want to do it the correct and secure way.

解决方案

All big websites don't save pictures to the database they store them in the disk. They save a reference to the picture's position in a table. And then link from there.

Why? Performance.

Pulling heavy content from a database is a huge performance bottleneck. And databases don't scale horizontally that well, so it would mean even a bigger problem. All big sites use static content farms to deal with static content such as images. That's servers who won't care less about your identity.

How do they keep the pictures really private you might ask? They don't.

The picture's link is, in itself, the address and the password. Let's take Facebook, for example. If I store a private picture on my account you should not be able to open it. But, as long as you have the correct address you can.

This picture is private. Notice the filename
10400121_87110566301_7482172_n.jpg
(facebook changes the url from time to time so the link may be broken)

It's non sequential. The only way to get the picture is to know it's address.
Based on a previous user photo you can't guess the next one.
It has a huge entropy so even if you start taking random wild guesses you'll have an extensive amount of failures and, if you do get to a picture, you won't be able to, from there, realize the owners identity which, in itself, is protection in anonymity.

Edit (why you should not store images in a "username" folder:
After your edit it became clear that you do intent to put files on disk and not on the database. This edit covers the new scenario.

Even though your logic (create a folder per user) seams more organized it creates problems when you start having many users and many pictures. Imagine that your servers have 1T disk space. And lets also imagine that 1T is more or less accurate with the load the server can handle.

Now you have 11 users, assume they start uploading at the same time and each will upload more than 100GB of files. When they reach 91GB each the server is full and you must start storing images on a different server. If that user/folder structure is followed you would have to select one of the users and migrate all of his data to a different server. Also, it makes a hard-limit on a user who can't upload more than 1T in files.

Should I store all files in the same folder, then?
No, big-sites generally store files in sequential folders (/000001/, /000002/, etc) having an x defined number of files per folder. This is mainly for file-system performance issues.

More on how many files in a directory is too many?

这篇关于上传和存储图片的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆