php - 如何在 MySQL 数据库中插入 pdf 文件,然后在 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();
}
?>
解决方案
你需要
将数据存储
TEXT
在 MySQL 的列中,而不是BLOB
,因为 base64 字符串是文本,而不是二进制数据。从数据库中查询该字段以获取 base64 数据
对base64数据进行解码,得到原始二进制数据。
发送该数据以供下载,为您的浏览器设置适当的标题以了解它是文件下载而不是网页。
从您的问题的更新来看,您现在似乎已经对 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;
推荐阅读
- python - 在python中获取一个变量在函数外工作
- arrays - 程序输出比数组中实际元素数多 1
- unity3d - unity 如何组合每个对象名称?
- java - 如何从 class.getResource() 释放资源
- python - 如何替换引用相同的类似书面值(赋予它们相同的值)?
- macos - Chrome 在新选项卡中将下载链接作为文件打开,而不是在 mac os 上下载文件
- android - Android Fragment 向 Android Activity 添加菜单
- sql-server - 在 SQL Server 中将 NULL 或 0.00 替换为 00.00
- php - Wordpress ACF php 关系
- math - 用 4 个点和指定的坡度角对人工地平进行编码