我试图创建一个XML文件,然后将其作为电子邮件发送,并强制下载,问题是XML文档的末尾包含一些随机数字,使其变得毫无用处。
代码:
header('Content-Disposition: attachment;filename=License.xml');
header('Content-Type: text/xml');
$document = new DOMDocument('1.0');
$document->formatOutput = true;
$element_account = $document->createElement("Account");
$attr_name = $document->createAttribute("Username");
$attr_pass = $document->createAttribute("Password");
$attr_key = $document->createAttribute("Key");
$attr_name->value = $user;
$attr_pass->value = $pass;
$attr_key->value = $key;
$element_account->appendChild($attr_name);
$element_account->appendChild($attr_pass);
$element_account->appendChild($attr_key);
$document->appendChild($element_account);
$file_to_attach = 'tmp/License'.$user.'.xml';
$document->save($file_to_attach);
require '../PHPMailer/PHPMailerAutoload.php';
$pemail = new PHPMailer();
$pemail->From = 'donotreply@OGServer.net';
$pemail->FromName = 'OGServer Licensing';
$pemail->Subject = 'Your OGServer License has arrived!';
$pemail->Body = 'Thank you for registering your product, you will find your License attached to the e-mail, if you have any questions about how to set up your license, you can view the tutorial here: http://ogserver.net/licensing/tutorial.html';
$pemail->AddAddress( $email );
$pemail->AddAttachment($file_to_attach, 'License.xml' );
$pemail->Send();
$filepath = realpath($file_to_attach);
echo readfile($file_to_attach);
在输出要附加的文件后输出这些数字。你在这里做:
echo readfile($file_to_attach);
只是readfile
返回读取的字节数,然后您回显该数字。引用标题为"返回值:"的部分
返回从文件中读取的字节数。如果发生错误,将返回FALSE,除非函数被调用为@readfile(),否则将打印错误消息。
由于readfile
已经将文件的内容输出到STDOUT,您只需在之后添加文件大小的整数(readfile
读取的字节)。
由于文件大小实际上并没有那么大,因此在这里使用readfile
几乎没有什么好处,因为它需要将文件放在磁盘上。
因此,您也可以将XML存储到一个字符串中:
$licenseXml = $document->saveXML();
然后将其附加到电子邮件中:
$pemail->AddStringAttachment($licenseXml, 'License.xml');
然后输出:
echo $licenseXml;
这应该做得同样好。