使用PHP和MySQL存储用户图像的最佳方法是什么? [英] What is the best way to store users images using PHP and MySQL?

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

问题描述

我想知道使用PHP和MySQL来存储用户上传头像等图像的最佳方法是什么?我应该从哪里开始?在这方面有一篇很好的文章吗?

I was wondering what is the best way to store a users upload images like an avatar and so on using PHP and MySQL? Where should I begin? And is there a good article on this?

推荐答案

最佳"取决于您的目标.

"Best" depends on what your goal is.

存储用户上传的图像的两种主要方法是将二进制内容作为BLOB放入数据库中,或者将图像存储到驱动器中的某个位置,然后将指示该图像所属位置的条目放入数据库中.

The two primary ways of storing user-uploaded images are either putting the binary content into the database as a BLOB, or storing the images to the drive somewhere and putting an entry into the database indicating which image belongs where.

将图像放置在数据库中的优点是不需要Web服务器上的任何文件系统权限,并且如果要在多个Web服务器上提供站点服务,则可以消除任何类型的同步问题.但是,随着时间的流逝,它会使您的数据库变得庞大,并且,如果您不正确地设计表,它绝对会破坏性能和可伸缩性.

Placing the images in the database has the advantage of not requiring any sort of filesystem permissions on the webserver, and removes any sort of syncing issues if you're serving up the site off of multiple webservers. However, over time it makes your database huge, and if you don't design your tables correctly, it can absolutely kill your performance and scalability.

将图像作为文件存储在文件系统中,具有额外的优势,使检索变得非常快速和高效,因为Web服务器非常擅长提供静态文件.

Storing the images as file on the file system has the added advantage of making retrieval extremely quick and efficient, since webservers are very good at serving static files.

编辑后添加

如果决定将文件内容存储在数据库中,则绝对不要将其放在需要快速访问的表中.例如,如果您有一个用户"表,几乎在每个页面浏览量上都在其中搜索该表,那么该表就不是放置文件内容的地方.而是,创建一个包含文件和相关元信息的单独的图像"或文件"表.

If you decide to store file content in the database, absolutely do not put it in a table that needs to be accessed quickly. If, for example, you have a "users" table that is searched on nearly every pageview, then that table is not the place to put your file contents. Instead, create a separate "images" or "files" table containing the file and related meta-information.

将每行很多字节放入表中会使该表的使用非常缓慢.您不希望表中使用大量东西的东西.

Putting a lot of bytes per row into a table makes that table very slow to work with. You don't want that kind of thing in tables that see heavy use.

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

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