我正在尝试发送附件但收到空白文件。 我做错了什么?
所以$_POST["screenshot"]
它是da:image/png;base64,....
格式的图像,这里似乎一切正常,它没有任何问题地到达,会发生什么?为什么我会收到一个空白文件?如何在没有保存之前正确发送它(注释区域)?
<?php
header("Content-type: application/json; charset=utf-8");
if(isset($_POST["screenshot"]) && strlen($_POST["screenshot"]) > 0){
$data = str_replace("data:image/png;base64,", "", $_POST["screenshot"]);
// $png = fopen("screenshot.png", "w");
// fwrite($png, base64_decode($data));
// fclose($png);
*/
/* ___________ E-MAIL _____________ */
$to = 'mymail@mail.com';
$subject = 'Test email';
$attachment = chunk_split($data);
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "From: domain@mail.com\r\nReply-To: mymail@mail.com" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// attachment
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: application/octet-stream; name=\"" . "cartolina.png" . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: base64" . $eol;
$headers .= "Content-Disposition: attachment" . $eol;
$headers .= $content . $eol;
$headers .= "--" . $separator . "--";
//SEND Mail
mail($to, $subject, "", $headers);
echo json_encode(array("ok"=>"evertyhing is ok. "));
} else {
echo json_encode(array("error"=>"not ok"));
}
?>