如何在 MySQL 数据库中插入一个 pdf 文件,然后在 php 中读取它?
How to insert a pdf file in a MySQL database and then read it in php?
我有一个 PHP 代码,允许我使用 SendGrid 通过电子邮件发送 pdf 文件,并将此文件作为 BLOB 插入 MySQL 数据库中。
一切正常,但插入数据库的文件始终是 [BLOB - 20,0 kio] 文件,我不知道它是否正确插入以及如何从数据库中检索它...
感谢您的帮助
<?php
$filename2 = 'test.pdf';
$file_encoded = base64_encode(file_get_contents("C:/wamp64/www/final/API_label/PDF/$filename2"));
$email->addAttachment($file_encoded, "application/pdf", "$filename2", "attachment");
$sendgrid = new \SendGrid('SG.Ebi-CnMATfeehrmw89O5CuSNfPk');
try {
// $response = $sendgrid->send($email);
// print $response->statusCode() . "\n";
// print_r($response->headers());
// print $response->body() . "\n";
} catch (Exception $e) {
// echo 'Caught exception: ' . $e->getMessage() . "\n";/
}
//Insertion of the values in the database
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = 'INSERT INTO DHL (submission_id, formID, identite, email, adresse, telephone, label, commercial_invoice)
VALUES (:submission_id, :formID, :NOM, :EMAIL, :ADRESSE, :TELEPHONE, :file_encoded, :COMMERCIAL_INVOICE)';
// Prepare query
$query = $db->prepare($sql);
$query->bindParam(':file_encoded', $file_encoded, PDO::PARAM_LOB);
// Execute query
$query->execute(array(':submission_id' => $submission_id, ':formID' => $formID, ':NOM' => $NOM, ':EMAIL' => $EMAIL, ':ADRESSE' => $ADRESSE, ':TELEPHONE' => $TELEPHONE,
':file_encoded' => $file_encoded, ':COMMERCIAL_INVOICE' => $COMMERCIAL_INVOICE));
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
?>
要下载的代码:
//PDO PART
include '../include/classe_PDO.php';
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = "SELECT * FROM `dhl` WHERE `submission_id` = '5094071540419221255'";
foreach ($db->query($sql) as $row) {
$filedata = $row['label']; //get base64 data from query result
$decoded = base64_decode($filedata); //decode base64 to binary
//set suitable HTTP response headers
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="label.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//output the binary file data in the body of the response
echo $decoded;
exit;
}
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
你需要
将数据存储在 MySQL 的 TEXT
列中,而不是 BLOB
,因为 base64 字符串是文本,而不是二进制数据。
从数据库中查询该字段以获取 base64 数据
对base64数据进行解码得到原始二进制数据
发送该数据以供下载,设置适当的 headers 让您的浏览器理解这是文件下载而不是网页。
从您问题的更新来看,您现在似乎已经对 SQL 部分进行了排序,所以在这里我将只展示一种更简单的下载数据的方法,而无需将文件写入首先清理服务器磁盘(稍后需要清理)。
$filedata = $row['label']; //get base64 data from query result
$decoded = base64_decode($filedata); //decode base64 to binary
//set suitable HTTP response headers
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="test.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//output the binary file data in the body of the response
echo $decoded;
exit;
我有一个 PHP 代码,允许我使用 SendGrid 通过电子邮件发送 pdf 文件,并将此文件作为 BLOB 插入 MySQL 数据库中。
一切正常,但插入数据库的文件始终是 [BLOB - 20,0 kio] 文件,我不知道它是否正确插入以及如何从数据库中检索它...
感谢您的帮助
<?php
$filename2 = 'test.pdf';
$file_encoded = base64_encode(file_get_contents("C:/wamp64/www/final/API_label/PDF/$filename2"));
$email->addAttachment($file_encoded, "application/pdf", "$filename2", "attachment");
$sendgrid = new \SendGrid('SG.Ebi-CnMATfeehrmw89O5CuSNfPk');
try {
// $response = $sendgrid->send($email);
// print $response->statusCode() . "\n";
// print_r($response->headers());
// print $response->body() . "\n";
} catch (Exception $e) {
// echo 'Caught exception: ' . $e->getMessage() . "\n";/
}
//Insertion of the values in the database
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = 'INSERT INTO DHL (submission_id, formID, identite, email, adresse, telephone, label, commercial_invoice)
VALUES (:submission_id, :formID, :NOM, :EMAIL, :ADRESSE, :TELEPHONE, :file_encoded, :COMMERCIAL_INVOICE)';
// Prepare query
$query = $db->prepare($sql);
$query->bindParam(':file_encoded', $file_encoded, PDO::PARAM_LOB);
// Execute query
$query->execute(array(':submission_id' => $submission_id, ':formID' => $formID, ':NOM' => $NOM, ':EMAIL' => $EMAIL, ':ADRESSE' => $ADRESSE, ':TELEPHONE' => $TELEPHONE,
':file_encoded' => $file_encoded, ':COMMERCIAL_INVOICE' => $COMMERCIAL_INVOICE));
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
?>
要下载的代码:
//PDO PART
include '../include/classe_PDO.php';
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = "SELECT * FROM `dhl` WHERE `submission_id` = '5094071540419221255'";
foreach ($db->query($sql) as $row) {
$filedata = $row['label']; //get base64 data from query result
$decoded = base64_decode($filedata); //decode base64 to binary
//set suitable HTTP response headers
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="label.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//output the binary file data in the body of the response
echo $decoded;
exit;
}
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
你需要
将数据存储在 MySQL 的
TEXT
列中,而不是BLOB
,因为 base64 字符串是文本,而不是二进制数据。从数据库中查询该字段以获取 base64 数据
对base64数据进行解码得到原始二进制数据
发送该数据以供下载,设置适当的 headers 让您的浏览器理解这是文件下载而不是网页。
从您问题的更新来看,您现在似乎已经对 SQL 部分进行了排序,所以在这里我将只展示一种更简单的下载数据的方法,而无需将文件写入首先清理服务器磁盘(稍后需要清理)。
$filedata = $row['label']; //get base64 data from query result
$decoded = base64_decode($filedata); //decode base64 to binary
//set suitable HTTP response headers
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="test.pdf"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//output the binary file data in the body of the response
echo $decoded;
exit;