我有一個 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();
}
?>
uj5u.com熱心網友回復:
你需要
將資料存盤
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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/311213.html
