php 如何在 MySQL 数据库中存储 PDF 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3785754/
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
How to store a PDF file in MySQL database?
提问by user389055
How to store a PDF file in a MySQL database using PHP?
如何使用 PHP 在 MySQL 数据库中存储 PDF 文件?
回答by AlexanderMP
Using BLOB (Binary Large Object) (longblob
datatype)
使用 BLOB(二进制大对象)(longblob
数据类型)
$fileHandle = fopen($fileUpload, "r");
$fileContent = fread($fileHandle, $fileUpload_size);
$fileContent = addslashes($fileContent);
$dbQuery = "INSERT INTO myBlobs VALUES ";
$dbQuery .= "('$fileContent')";
The full tutorial available here
完整的教程在这里
but it's strongly recommended to store files on the file system, and just add a reference in the DB (a field with the file path and name). Several reasons:
但强烈建议将文件存储在文件系统上,只需在数据库中添加一个引用(包含文件路径和名称的字段)。几个原因:
- Faster
- Easier to access (don't need any special application)
- Faster backups
- Less space
- 快点
- 更容易访问(不需要任何特殊应用程序)
- 更快的备份
- 更小的空间
回答by Paul Whelan
回答by Russell Dias
As others mentioned, you can use a BLOB type.
正如其他人提到的,您可以使用 BLOB 类型。
Alternatively, what you can also do is save the PDF in the file system and save the relative link to it into the database. This is the same principle that applies for saving things such as Thumbnails and other data types that may be unique to the user and are expensive in terms of resources.
或者,您还可以将 PDF 保存在文件系统中,并将其相关链接保存到数据库中。这与适用于保存诸如缩略图和其他数据类型之类的内容的原则相同,这些内容可能对用户来说是唯一的,并且在资源方面是昂贵的。
回答by John Wordsworth
If you are simply looking to store uploaded PDF files on your server, it's more common to copy the file to a given directory with a unique filename and simply store the full path of that file in your MySQL table.
如果您只是想在您的服务器上存储上传的 PDF 文件,更常见的是将文件复制到具有唯一文件名的给定目录,并将该文件的完整路径存储在您的 MySQL 表中。
If you are definitely looking to store the full binary data of the file in your MySQL database, then you will have to do a little more work to put the binary data into a BLOB field in MySQL and then to turn it back into a file when you pull it out again at a later date.
如果您确实希望将文件的完整二进制数据存储在您的 MySQL 数据库中,那么您将需要做更多的工作来将二进制数据放入 MySQL 的 BLOB 字段中,然后在你稍后再把它拉出来。
You will probably want to store not only the binary data for the file in your MySQL table, but also the filename, filetype and possibly even the size of the file (for listing on your webpage). You will probably want a table such as;
您可能不仅要在 MySQL 表中存储文件的二进制数据,还要存储文件名、文件类型,甚至可能是文件的大小(用于在您的网页上列出)。您可能需要一张桌子,例如;
CREATE TABLE files (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
filename VARCHAR(128) NOT NULL,
mimetype VARCHAR(64) NOT NULL,
data MEDIUMBLOB NOT NULL
);
In your PHP, you can then insert an uploaded file with something like the following;
在你的 PHP 中,你可以插入一个上传的文件,如下所示;
$filePointer = fopen($_FILES['fileUpload']['tmp_name'], 'r');
$fileData = fread($filePointer, filesize($_FILES['fileUpload']['tmp_name']));
$fileData = addslashes($fileData);
$sql = "INSERT INTO files (filename, mimetype, data) VALUES( $fileName, $fileType, $fileData )";
Getting the file back out again will required a dedicated script that selects the appropriate file and then uses a series of 'header' commands to push that file back down to the browser with in a form that the browser knows how to handle it.
再次取出文件将需要一个专用脚本来选择适当的文件,然后使用一系列“标题”命令将该文件以浏览器知道如何处理的形式推送回浏览器。
You can read a full tutorial about this here.
您可以在此处阅读有关此内容的完整教程。