PHP显示BLOB数据?
如何使用 PHP 显示 BLOB 数据?我已将 BLOB 输入到数据库中,但如何检索它?任何例子都会很棒。
How would I display BLOB data with PHP? I've entered the BLOB into the DB, but how would I retrieve it? Any examples would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我考虑投票关闭这个重复项,但标题相当不错,并且浏览其他问题,我没有找到一般问题的完整答案。这类问题暴露了对 HTTP 基础知识缺乏了解,所以我写了这个长答案。我已经掩盖了一点,但任何理解以下内容的人可能都不需要问这样的问题。或者如果他们这样做了,他们就能够提出更具体的问题。
首先 - 如果您在数据库中存储图像或其他文件,请停止并重新考虑您的架构。 RDBMS 并未真正针对处理 BLOB 进行优化。有许多(非关系型)数据库专门用于处理文件。它们被称为文件系统,并且它们非常擅长于此。至少 95% 的情况下,我发现常规文件卡在 RDBMS 中,这是毫无意义的。因此,首先,考虑不将文件数据存储在数据库中,使用文件系统,并在数据库中存储一些小数据(如果必须的话,路径,通常您可以组织文件系统,因此您所需要的只是唯一的 ID)。
那么,您确定要将 Blob 存储在数据库中吗?
在这种情况下,您需要了解 HTTP 的工作原理。无需赘述,只要某个客户端请求 URL(发出 HTTP 请求),服务器就会使用 HTTP 响应进行响应。 HTTP 响应有两个主要部分:标头和数据。两个部分由两个连续的换行符分隔。
线路上的标头是简单的纯文本键/值对,如下所示:
并由换行符分隔。
数据基本上是 BLOB。这只是数据。数据的解释方式由客户端根据其附带的 Content-Type 标头的值决定。 Content-Type 标头指定数据部分中包含的数据的互联网媒体类型。
看看它是否有效
这并没有什么神奇之处。对于常规 HTML 页面,整个响应是人类可读的。尝试以下操作:
您将看到类似以下内容:
现在输入:(
后跟回车)。
您刚刚发出了一个非常简单的 HTTP 请求!您可能已收到回复。看看回复。您将看到所有标题,后跟一个空行,然后是 google 主页的 HTML 代码。
那又怎样?
现在您知道网络服务器是做什么的了。它们接受请求(如
GET /
),并返回响应(由标头后跟一个空行(两个连续的换行符)和数据组成)。现在,是时候认识到:
您的 Web 应用程序实际上只是一个自定义的 Web 服务器
您编写的所有代码都会接受任何请求,并将其转换为 HTTP 响应。所以你基本上只是制作一个专门版本的 apache、IIS、nginx、lighty 等等。
现在,Web 服务器通常处理请求的默认方式是在目录(文档根目录)中查找文件,查看该文件以确定要发送哪些标头,然后发送这些标头,然后发送文件内容。
但是,虽然您的 Web 服务器对文件系统中的文件执行了所有这些操作,但它完全不了解 RDBMS 中的某些 BLOB。所以你必须自己做。
如果您知道 BLOB 的内容是一个 JPG 图像,应该根据同一个表中的“名称”列来命名,您可能会执行以下操作:(
如果您想提示浏览器应该下载该文件要显示它,您可以使用附加标头,例如:
header('Content-Disposition: Attachment; filename="' . $row['name'].'"');
)PHP 很聪明足以提供 header() 函数,该函数设置标头,并确保首先发送它们(并与数据分开)。设置完标头后,您只需发送数据即可。
只要您的标头为客户端提供了有关如何处理数据有效负载的足够信息,一切就都很顺利了。
万岁。
I considered voting to close this a a duplicate, but the title is pretty good, and looking through other questions, I don't find a complete answer to a general question. These sorts of questions betray an absence of understanding of the basics of HTTP, so I wrote this long answer instead. I've glossed over a bit, but anyone who understands the following probably wouldn't need to ask a question like this one. Or if they did, they'd be able to ask a more specific question.
First - If you're storing images or other files in the database, stop and reconsider your architecture. RDBMSes aren't really optimized to handle BLOBs. There are a number of (non-relational) databases that are specifically tuned to handle files. They are called filesystems, and they're really good at this. At least 95% of the time that I've found regular files stuck in a RDBMS, it's been pointless. So first off, consider not storing the file data in the database, use the filesystem, and store some small data in the database (paths if you must, often you can organize your filesystem so all you need is a unique id).
So, you're sure you want to store your blob in the database?
In that case, you need to understand how HTTP works. Without getting into too much detail, whenever some client requests a URL (makes an HTTP Request), the server responds with a HTTP Response. A HTTP response has two major parts: the headers, and the data. The two parts are separated by two consecutive newlines.
Headers, on the wire, are simple plain-text key/value pairs that look like:
and are separated by a newline.
The data is basically a BLOB. It's just data. The way that data is interpreted is decided (by the client) based on the value of the Content-Type header that accompanies it. The Content-Type header specifies the Internet Media Type of the data contained in the data section.
See it work
There's nothing magic about this. For a regular HTML page, the whole response is human readable. Try the following:
You'll see something like:
Now type:
(followed by return).
You've just made a very simple HTTP request! And you've probably received a response. Look at the response. You'll see all the headers, followed by a blank line, followed by the HTML code of the google home page.
So what?
So now you know what web servers do. They take requests (like
GET /
), and return responses (comprised of headers followed by a blank line (two consecutive newlines) followed by data).Now, it's time to realize that:
Your web application is really just a customized web server
All that code you write takes whatever the request is, and translates it into an HTTP response. So you're basically just making a specialized version of apache, or IIS, or nginx, or lighty, or whatever.
Now, the default way that a web server usually handles requests is to look for a file in a directory (the document root), look at it to figure out which headers to send, and then send those headers, followed by the file contents.
But, while your webserver does all that magically for files in the filesystem, it is completely ignorant of some BLOB in an RDBMS. So you have to do it yourself.
If you know the contents of your BLOB are, say, a JPG image that should be named based on a "name" column in the same table, you might do something like:
(If you wanted to hint that browser should download the file instead of display it, you might use an additional header like:
header('Content-Disposition: attachment; filename="' . $row['name'].'"');
)PHP is smart enough to provide the header() function, which sets headers, and makes sure they're sent first (and separated form the data). Once you're done setting headers, you just send your data.
As long as your headers give the client enough information about how to handle the data payload, everything is hunkey-dorey.
Hooray.
简单的例子:
Simple example: