简体   繁体   English

如何使用php中的addFromString函数创建带有PDF文件的zip文件?

[英]How to create a zip file with PDF files with the addFromString function in php?

I have this code broken down into two parts:我将这段代码分为两部分:

  • The first one create me a zip file empty and download it.第一个为我创建一个空的 zip 文件并下载它。
  • The second one download me from my database a PDF file that I decoded in base64.第二个从我的数据库中下载我用 base64 解码的 PDF 文件。

Both work well separately.两者分开工作都很好。 I would like to be able to merge them and download in zip format two PDF files that are in my database.我希望能够合并它们并以 zip 格式下载我数据库中的两个 PDF 文件。

I would like to use the addFromString function to not create a folder on my server but to download the zip file directly for the user.我想使用addFromString函数不在我的服务器上创建文件夹,而是直接为用户下载 zip 文件。

Can you help me please?你能帮我吗? Thanks for your help谢谢你的帮助

<?php


include '../include/classe_PDO.php';

$suivi = $_POST['suivi'];

// //ZIP PART
//Name zip folder with date
$dirzip = date('Ymd His')."_andromeda";

// Zip creation        
$nomzip = "C:/wamp64/www\Dylan/SAV/final/include/$dirzip.zip"; 
$zip = new ZipArchive;
if($zip -> open($nomzip, ZipArchive::CREATE ) === TRUE)
{ 
    $dir = opendir($chemin); 
    while($fichier = readdir($dir)) 
    { 
        if(is_file($chemin.$fichier)) 
        { 
          $zip -> addFromString($file, $decoded);
        } 
    } 
    $zip ->close(); 
} 

//Zip download
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($nomzip).'"');
header('Content-Length: ' . filesize($nomzip));

flush();
readfile($nomzip);  
         

//DOWNLOAD PART
 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` = :suivi";

  // Prepare query
  $query = $db->prepare($sql);

  // Execute query
  $query->execute(array(':suivi' => $suivi));

  foreach  ($query as $row) {
    
          $filedata = $row['label_envoi']; //get base64 data from query result
          $decoded = base64_decode($filedata); //decode base64 to binary

          $filedata2 = $row['label_retour']; //Second column
          $decoded2 = base64_decode($filedata2); 
          
          //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;
          
  }


} catch (PDOException $e) {
  echo 'Error: ' . $e->getMessage();
}        



?>

You need to loop through your database rows and add each piece of decoded file data to the zip file.您需要遍历数据库行并将每个解码的文件数据添加到 zip 文件中。 Then download the zip at the end.然后下载最后的zip。

For example:例如:

include '../include/classe_PDO.php';

$suivi = $_POST['suivi'];

// //ZIP PART
//Name zip folder with date
$dirzip = date('Ymd His')."_andromeda";

// Zip creation        
$nomzip = "C:/wamp64/www/Dylan/SAV/final/include/$dirzip.zip"; 
$zip = new ZipArchive;

if ($zip->open($nomzip, ZipArchive::CREATE ) === TRUE)
{
  try {
    //connect to DB
    $db = new db('mysql:dbname=jotform; host=localhost', 'root', '');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //prepare and execute query
    $sql = "SELECT * FROM `dhl` WHERE `submission_id` = :suivi";
    $query = $db->prepare($sql);
    $query->execute(array(':suivi' => $suivi));

    foreach ($query as $row) {
      //decode base64 to binary
      $decoded = base64_decode($row['label_envoi']); 
      $decoded2 = base64_decode($row['label_retour']);
    
      //add to zip
      //N.B. if there could be more than one row returned by the database, you'll need to ensure the PDFs have unique names instead of these hard-coded ones
      $zip->addFromString("label1.pdf", $decoded);
      $zip->addFromString("label2.pdf", $decoded2);
    }
  }
  catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
  }

  //Zip download
  header('Content-Type: application/zip');
  header('Content-Disposition: attachment; filename="'.basename($nomzip).'"');
  header('Content-Length: ' . filesize($nomzip));

  flush();
  readfile($nomzip);
}
else
{
  echo "Error - Zip file could not be created";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM