php 在网站上上传和存储图片的最佳方式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8922056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to upload and store pictures on the site?
提问by niko
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.
但我想这样做。这是正确的吗?例如,一个名为www.photos.com
. 当用户上传图片时,我会创建一个用户名文件夹并将这些图片保存在该文件夹中。
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.
我相信我们可以使用php file concepts
. 所以当一个新用户上传他的图片或文件时,我想用他的名字创建一个目录。
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?
示例:如果用户名是john,我会在photos.com上创建一个这样的目录www.photos.com/john/
,然后在他上传图片时将他的所有图片保存到该目录中。这是正确的方法吗?
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.
我这里没有人对将文件保存到服务器有很好的了解,所以请告诉我如何做到这一点?我想以正确和安全的方式做到这一点。
回答by Frankie
All big websites don't save pictures to the databasethey 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.
图片的链接本身就是地址和密码。让我们以 Facebook 为例。如果我将私人图片存储在我的帐户中,您将无法打开它。但是,只要您有正确的地址就可以。
This picture is private.Notice the filename10400121_87110566301_7482172_n.jpg
(facebook changes the url from time to time so the link may be broken)
这张照片是私人的。注意文件名10400121_87110566301_7482172_n.jpg
(facebook 会不时更改 url,因此链接可能会损坏)
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.
即使您的逻辑(为每个用户创建一个文件夹)接缝更有条理,但当您开始拥有许多用户和许多图片时,它会产生问题。假设您的服务器有 1T 磁盘空间。并且让我们想象一下,对于服务器可以处理的负载,1T 或多或少是准确的。
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.
现在您有 11 个用户,假设他们同时开始上传,每个用户将上传超过 100GB 的文件。当它们每个达到 91GB 时,服务器已满,您必须开始将图像存储在不同的服务器上。如果遵循该用户/文件夹结构,则必须选择其中一个用户并将其所有数据迁移到不同的服务器。此外,它对不能上传超过 1T 文件的用户进行了硬限制。
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.
那么我应该将所有文件存储在同一个文件夹中吗?
不,大型站点通常将文件存储在顺序文件夹(/000001/、/000002/ 等)中,每个文件夹具有 x 定义的文件数。这主要是针对文件系统性能问题。
More on how many files in a directory is too many?
更多关于一个目录中有多少文件太多?
回答by Sergio Tulentsev
It is usually a bad idea to store images in your database (if your site is popular). Database is, traditionally, one of main bottlenecks in most any application out there. No need to load it more than necessary. If images are in the filesystem, many http servers (nginx, for example) will serve them most efficiently.
将图像存储在您的数据库中通常是一个坏主意(如果您的网站很受欢迎)。传统上,数据库是大多数应用程序的主要瓶颈之一。没有必要加载它超过必要的。如果图像在文件系统中,许多 http 服务器(例如nginx)将最有效地为它们提供服务。
The biggest social network in Russia, Vkontaktedoes exactly this: store images in the filesystem.
作为俄罗斯最大的社交网络,Vkontakte正是这样做的:将图像存储在文件系统中。
Another big social network implemented a sophisticated scalable blob storage. But it's not available to the public, AFAIK.
另一个大型社交网络实施了复杂的可扩展 blob 存储。但它不对公众开放,AFAIK。
Summary of this answer: don't store blobs in the database.
此答案的摘要:不要在数据库中存储 blob。
回答by Sergio Tulentsev
Image management may best be achieved by physically uploading images to the server and then recording file location and image details in a database. Subsequently, a Search Form could be configured to permit the user to do a text search, part number search, or other queries. A PHP script could be written to produce a valid HTML image tag based on data found in the table.
最好通过将图像物理上传到服务器,然后在数据库中记录文件位置和图像详细信息来实现图像管理。随后,可以配置搜索表单以允许用户进行文本搜索、零件编号搜索或其他查询。可以编写一个 PHP 脚本来根据表中找到的数据生成一个有效的 HTML 图像标记。
uploading images into a MySQL? BLOB field is such a bad idea such image data is generally problematic if the images are much larger than thumbnails. If the images are large, you can end up having to copy/paste one SQL INSERT statement at a time (into phpMyAdmin). If the images are large and the SQL INSERT statement is broken into two lines by your text editor, you'll never be able to restore the image.
将图像上传到 MySQL?BLOB 字段是一个坏主意,如果图像比缩略图大得多,则此类图像数据通常会出现问题。如果图像很大,您最终可能不得不一次复制/粘贴一个 SQL INSERT 语句(到 phpMyAdmin)。如果图像很大并且 SQL INSERT 语句被文本编辑器分成两行,您将永远无法恢复图像。
回答by Your Common Sense
is this the right way to do
这是正确的做法吗
Yes.
The only thing I'd suggest to use not name but id.www.photos.com/albums/1234/
would be okay for starter.
是的。
我唯一建议不要使用名称而是使用 id。www.photos.com/albums/1234/
初学者没问题。