所以我正在尝试生成一个Excel文件并将其保存到服务器。这与PHPExcel完美配合。下一步是读取文件,将其作为附件通过电子邮件发送,然后删除
由于某些原因,PHP无法将xlsx识别为正确的文件:
/* excel is generated before here */
$filename = "/results/excel/export-" . $today . ".xlsx";
$writer = new PHPExcel_Writer_Excel2007($exc);
$writer->save($filename);
/* so far so good; the file is created and exists on the server */
$to = "someone@domain.com";
$sendermail = "no-reply@domain.com";
$from = "Sender <" . $sendermail . ">";
$subject = "Email with attachment";
$message = "Here you go";
$file = $filename;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers = "From: $from";
$headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary=" {$mime_boundary}"";
$message = "--{$mime_boundary}n" . "Content-Type: text/plain; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" . $message . "nn";
if(is_file($file)) {
$message .= "--{$mime_boundary}n";
$fp = @fopen($file, "rb");
$data = @fread($fp, filesize($file));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name="" . basename($file). ";n" . "Content-Transfer-Encoding: base64nn" . $data . "nn";
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
mail($to, $subject, $message, $headers, $returnpath);
} else {
echo("This is not a file: " . $file);
}
它总是给我"这不是一个文件…"。我如何读取XLSX文件?
这里有一个提示,可以提示excel并将文件作为附件发送,而无需将文件保存在服务器上:
$objWriter = PHPExcel_IOFactory::createWriter ($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
$data = ob_get_contents();
$sendmail=new ExcelMail($data); //this is your mail class; if you use zend mail use addattachement($data); and Zend_Mime
输出的消息表示is_file()
失败。这可能有很多原因,但据我所知,不是因为文件格式。因此,我建议研究以下内容:
-
文件真的在那里吗?你说你保存了文件,但你必须确保它确实是。
-
xslx文件使用的文件路径是否正确?
-
xslx文件的文件路径是实际的xml文件,而不是指向它的符号链接吗?
-
xslx文件的权限是否设置为允许php脚本访问它们?
我相信这一定是其中一个问题。。。
好吧,我发现了它是什么$filename实际上是直接的(http://...)指向文件的链接,而不是相对链接。把它改成相对的,它起作用了:)。