如何使用 PHP 将 .pdf 文件作为 BLOB 存储到 MySQL 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4813913/
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 .pdf files into MySQL as BLOBs using PHP?
提问by xcodemaddy
How to store .pdf files into MySQL as BLOBs from PHP?
如何将 .pdf 文件作为来自 PHP 的 BLOB 存储到 MySQL 中?
回答by GordonM
EDITED TO ADD: The following code is outdated and won't work in PHP 7. See the note towards the bottom of the answer for more details.
编辑添加:以下代码已过时,无法在 PHP 7 中使用。有关更多详细信息,请参阅答案底部的注释。
Assuming a table structure of an integer ID and a blob DATA column, and assuming MySQL functions are being used to interface with the database, you could probably do something like this:
假设一个整数 ID 和一个 blob DATA 列的表结构,并假设使用 MySQL 函数与数据库交互,你可能会做这样的事情:
$result = mysql_query 'INSERT INTO table (
data
) VALUES (
\'' . mysql_real_escape_string (file_get_contents ('/path/to/the/file/to/store.pdf')) . '\'
);';
A word of warning though, storing blobs in databases is generally not considered to be the best idea as it can cause table bloat and has a number of other problems associated with it. A better approach would be to move the file somewhere in the filesystem where it can be retrieved, and store the path to the file in the database instead of the file itself.
不过,警告一下,在数据库中存储 blob 通常不被认为是最好的主意,因为它会导致表膨胀并有许多其他相关的问题。更好的方法是将文件移动到文件系统中可以检索到的某个位置,并将文件的路径而不是文件本身存储在数据库中。
Also, using mysql_* function calls is discouraged as those methods are effectively deprecated and aren't really built with versions of MySQL newer than 4.x in mind. You should switch to mysqli or PDO instead.
此外,不鼓励使用 mysql_* 函数调用,因为这些方法实际上已被弃用,并且并不是真正使用比 4.x 更新的 MySQL 版本构建的。您应该改用 mysqli 或 PDO。
UPDATE: mysql_* functions are deprecatedin PHP 5.x and are REMOVED COMPLETELY IN PHP 7!You now have no choice but to switch to a more modern Database Abstraction (MySQLI, PDO). I've decided to leave the original answer above intact for historical reasons but don't actually use it
更新:mysql_* 函数在 PHP 5.x中已弃用,并在 PHP 7 中完全删除!您现在别无选择,只能切换到更现代的数据库抽象(MySQLI、PDO)。由于历史原因,我决定保留上面的原始答案,但实际上并不使用它
Here's how to do it with mysqli in procedural mode:
以下是在程序模式下使用 mysqli 执行此操作的方法:
$result = mysqli_query ($db, 'INSERT INTO table (
data
) VALUES (
\'' . mysqli_real_escape_string (file_get_contents ('/path/to/the/file/to/store.pdf'), $db) . '\'
);');
The ideal way of doing it is with MySQLI/PDO prepared statements.
这样做的理想方法是使用 MySQLI/PDO 准备好的语句。
回答by momoren
In regards to Gordon M's answer above, the 1st and 2nd parameter in mysqli_real_escape_string ()
call should be swapped for the newer php versions,
according to: http://php.net/manual/en/mysqli.real-escape-string.php
关于上面 Gordon M 的回答,mysqli_real_escape_string ()
调用中的第一个和第二个参数应该交换为更新的 php 版本,根据:http: //php.net/manual/en/mysqli.real-escape-string.php
回答by AbdoM
//Pour inserer :
$pdf = addslashes(file_get_contents($_FILES['inputname']['tmp_name']));
$filetype = addslashes($_FILES['inputname']['type']);//pour le test
$namepdf = addslashes($_FILES['inputname']['name']);
if (substr($filetype, 0, 11) == 'application'){
$mysqli->query("insert into tablepdf(pdf_nom,pdf)value('$namepdf','$pdf')");
}
//Pour afficher :
$row = $mysqli->query("SELECT * FROM tablepdf where id=(select max(id) from tablepdf)");
foreach($row as $result){
$file=$result['pdf'];
}
header('Content-type: application/pdf');
echo file_get_contents('data:application/pdf;base64,'.base64_encode($file));