我有這段代碼,它允許我從 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("[email protected]", "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");
uj5u.com熱心網友回復:
除非我誤解了什么,這兩個臨時檔案似乎沒有添加任何值并且可以洗掉 - 相反,您可以直接使用您寫入檔案的變數作為流程后續步驟的輸入。
這個版本應該完成相同的程序而不寫入任何檔案:
//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';
$filename2 = $envoi.'.pdf';
//We put the content of the tags in the variable $DHL
$DHL = multiSplit($output);
//We write the content of the tags
$print1 = print_r($DHL[1], true).PHP_EOL;
//Decode pdf content
$pdf_decoded = base64_decode($print1);
//SendGrid Part
$email = new Mail();
$email->setFrom("[email protected]", "Sender");
$email->addTo("$EMAIL", "Recipient");
$email->setSubject("Etiquettes DHL");
$email->addContent("text/html", "<strong>Hello</strong>");
$file_encoded = base64_encode($pdf_decoded);
$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();
}
我不清楚你為什么base64_decode要讀取第一個檔案,因為沒有證據表明它開始是 base64 編碼的(據我所知,這只是某些 XML 上的 print_r 陳述句的結果)。因此我也洗掉了它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/346234.html
上一篇:將base64字串轉換為pdf
