所以我正在尝试生成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.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\" {$mime_boundary}\"";
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
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: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
mail($to, $subject, $message, $headers, $returnpath);
} else {
echo("This is not a file: " . $file);
}
总是给我“这不是文件......”。我如何阅读XLSX文件?
答案 0 :(得分:1)
这里有一个提示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
答案 1 :(得分:0)
输出的消息表示is_file()
失败。这可能由于多种原因而发生,但据我所知,并不是因为文件格式。因此,我建议您研究以下内容:
文件实际存在吗?你说你保存了文件,但你必须确保它确实存在。
xslx文件使用的文件路径是否正确?
xslx文件的文件路径是实际的xml文件而不是符号链接吗?
是否设置了xslx文件的权限,以便允许php脚本访问它们?
我认为这必须是其中一个问题......
答案 2 :(得分:0)
好的,我发现它是什么。 $ filename实际上是文件的直接(http:// ...)链接,而不是相对链接。将其更改为相对的并且有效:)。