【发布时间】:2021-12-13 12:31:45
【问题描述】:
我有这段代码可以让我从 base64 编码的 XML 文件中检索字符串。 我把它放在一个 TXT 文件中,然后我对其进行解码以获得所需的 PDF。 然后我将此 PDF 作为附件发送并在数据库中插入 base64 字符串。然后我从文件夹中删除 TXT 文件和 PDF。
一切正常。但我想要一个解决方案,不在服务器上创建这些文件然后删除它们。
我正在寻找更安全的解决方案,我尝试了tmpfile 功能但文件没有通过邮件发送,因为它被直接删除。
你能帮帮我吗?
<?php
//Function to keep only the content of the <GraphicImage> tags
function multiSplit($string)
{
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach ($cols as $col)
{
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$envoi = $submission_id.'_envoi';
//We put the content of the tags in the variable $DHL
$DHL = multiSplit($output);
//We create two unique file names
$filename1 = $envoi.'.txt';
$filename2 = $envoi.'.pdf';
//We write the content of the tags in the txt file
$file1 = "D:/Data/Sites/sav/API_label/TXT/$filename1";
file_put_contents($file1, print_r($DHL[1], true) . PHP_EOL, FILE_APPEND | LOCK_EX);
$pdf_base64 = $file1;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ("D:/Data/Sites/sav/API_label/PDF/$filename2",'w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
// echo 'Done';
//SendGrid Part
$email = new Mail();
$email->setFrom("test@test.com", "Sender");
$email->addTo("$EMAIL", "Recipient");
$email->setSubject("Etiquettes DHL");
$email->addContent("text/html", "<strong>Hello</strong>");
$file_encoded = base64_encode(file_get_contents("D:/Data/Sites/sav/retours/API_label/PDF/$filename2"));
$email->addAttachment($file_encoded, "application/pdf", "$filename2", "attachment");
$sendgrid = new \SendGrid('SG.m7481IOix24');
try {
$response = $sendgrid->send($email);
} 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', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = 'INSERT INTO DHL (submission_id, formID, identite, email, adresse, telephone, label_envoi, commercial_invoice)
VALUES (:submission_id, :formID, :NOM, :EMAIL, :ADRESSE, :TELEPHONE, :file_encoded, :COMMERCIAL_INVOICE)';
// Prepare query
$query = $db->prepare($sql);
// 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();
}
//Deleting txt and pdf files
//PDF
unlink("D:/Data/Sites/sav/API_label/PDF/$filename2");
//TXT
unlink("D:/Data/Sites/sav/API_label/TXT/$filename1");
【问题讨论】:
-
我看不出写入文件有什么作用。为什么不将 print_r 输出放入变量而不是文件?此外,这似乎不会生成 PDF 数据
-
你想让我用变量替换TXT文件吗?但最终解码后的 PDF 文件仍将是创建它的物理文件,对吗?解码后的文件为PDF文件
-
如果你想要@ADyson,我可以放txt和pdf文件的截图