首页 > 解决方案 > 如何在 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();
}         


?>

标签: phpsqlpdf

解决方案


你需要

  1. 将数据存储TEXT在 MySQL 的列中,而不是BLOB,因为 base64 字符串是文本,而不是二进制数据。

  2. 从数据库中查询该字段以获取 base64 数据

  3. 对base64数据进行解码,得到原始二进制数据。

  4. 发送该数据以供下载,为您的浏览器设置适当的标题以了解它是文件下载而不是网页。

从您的问题的更新来看,您现在似乎已经对 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;

推荐阅读