php图像存储-安全性
我想我有两种方法在 php 应用程序中存储图像:要么将它们存储在数据库中,要么存储在具有唯一名称的文件夹中。考虑到我想最小化数据库连接,如何保护包含用户上传图像的文件夹?
所以基本上,每个用户都会有一个个人资料,并将他们的照片上传到网站,以便与他们想要的任何人分享。我计划将它们存储如下:
/root/somefolder/{userid}{uniquenumber}.jpg
如果我配置 htaccess 以阻止访问当前文件夹,就足够了吗?或者我应该采取其他安全预防措施吗?
干杯
I guess I have 2 approaches to store images in a php application: either to store them in a database or to a folder with unique names. Considering that I want to minimize the database connections, how can I secure a folder that has images uploaded by the users?
So basically, each user will have a profile and will upload their photos to the website to share them with whoever they want. I plan to store them as follows:
/root/somefolder/{userid}{uniquenumber}.jpg
If I configure the htaccess to prevent access to the current folder, will it be enough? Or should I have some other security precautions?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实需要一些额外的安全性,可以将文件存储在网站公共路径之外的文件夹中。构建一个页面,例如 http://example.com/get_photo/?id=12345,其中该页面验证用户是否已登录并有权访问照片,然后找到实际的文件名并将该文件包含在响应中。请记住,您需要为您的图像格式发出正确的 MIME 类型标头。
If you do want some additional security, you can store your files in a folder outside the web site's public path. Build a page such as http://example.com/get_photo/?id=12345 where that page verifies the user is logged in and has authority for the photo, then finds the actual filename and includes that file in the response. Remember that you will need to issue the correct MIME-type header for your image format.
实际上,许多数据库引擎无论如何都会将 BLOB 数据存储为单独的文件,因此只需首先将图像存储为文件即可。它将使您的数据库变得更小,更易于管理,更易于备份,并且更易于在紧急情况下恢复。
Actually, many database engines will store BLOBs of data as separate files anyway, so just store your images as files in the first place. It will make your database much smaller, easier to administer, easier to backup and easier to restore in an emergency.
数据库针对存储小数据而不是大 blob 进行了优化。如果让普通操作系统和 Web 服务器处理文件传输,您将获得更好的性能。使用普通文件也可以让您不必重新实现 HTTP 缓存标头和范围请求的处理。
但无论哪种方式,这些资源都需要 URL,因此问题的第二部分仍然相关。
我认为这就足够了。只要独特的部分很长(例如,“somefolder/738b3093b898654bd3bbb9e3770e7fc0.jpg”)并且不遵循模式,就无法猜测它。
但除非您确实需要文件名中的用户 ID,否则请避免使用它。 Facebook 将用户个人资料 ID 包含在照片文件名的一部分中,因此当人们在其他地方共享照片文件时,可以发现他们的 Facebook 个人资料和真实姓名。我已经看到它在多个在线社区中引起跟踪和骚扰。这不是用户的错——他们不知道在某个地方发布照片会泄露自己或朋友的真实姓名。
另一个考虑因素是,随着这样的系统的增长并且它会获得大量文件,如果它们仅位于一个文件夹中,那么这对文件系统来说是一个可怕的负担。您可以通过在子文件夹中重复或拆分部分名称来轻松防止这种情况。例如,“somefolder/7e/7e8b3093b898654bd3bbb9e3770e7fc0.jpg”。
Databases are optimized for storing small pieces of data rather than big blobs. You'll get better performance if you let the normal OS and web server handle file delivery. Using normal files saves you from having to re-implement handling of HTTP cache headers and range requests too.
But either way, these resources need URLs, so the second part of your question is still relevant.
I should think that will be enough. As long as the unique part is long (e.g., "somefolder/738b3093b898654bd3bbb9e3770e7fc0.jpg") and doesn't follow a pattern there is no way to guess it.
But unless you really need the userid in the file name, avoid it. Facebook includes the user profile ID is part of the photo file name, so when people share the photo files somewhere else it allows discovery of their Facebook profile and real name. I've seen it cause stalking and harrassment in multiple online communities. It's not the user's fault -- they have no idea they're giving away their real name or friend's real name by posting a photo somewhere.
Another consideration is that as such a system grows and it gets a massive number of files, it's an awful burden on the file system if they're in just one folder. You can prevent that easily by repeating or splitting part of the name in subfolders. E.g., "somefolder/7e/7e8b3093b898654bd3bbb9e3770e7fc0.jpg".