我在磁盘上有一个pdf文件,当他们向php脚本发出请求时我需要发送给用户,这样做的最佳方式是什么?
由于
答案 0 :(得分:54)
答案 1 :(得分:34)
以下是使用PHP发送文件所需的内容:
$filename = "whatever.jpg";
if(file_exists($filename)){
//Get file type and set it as Content Type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: ' . finfo_file($finfo, $filename));
finfo_close($finfo);
//Use Content-Disposition: attachment to specify the filename
header('Content-Disposition: attachment; filename='.basename($filename));
//No cache
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
//Define file size
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
exit;
}
正如Julian Reschke所评论的那样,经过验证的答案可能有效,但它充满了无用的标题。 内容类型应设置为文件的实际类型,或者某些浏览器(尤其是移动浏览器)可能无法正确下载。
答案 2 :(得分:3)
如果您使用的是Apache或Lighty,那么从性能的角度来看,“最佳”方法是使用X-Sendfile
标头。请参阅本教程:http://www.jasny.net/articles/how-i-php-x-sendfile/
答案 3 :(得分:1)
好的,所以我不是PHP的专家,我只能把PHP的一些其他片段放在一起来实现我需要它做的事情,我想我最好在几个方面发布这个解决方案论坛问了同样的问题,但我自己无法工作。似乎没有任何解决方案,所以在这里。这个对我有用...
好的,首先,我创建了PDF表单并添加了一个按钮,然后提交表单。在此提交表单的操作中,我告诉它PDF格式的完整文档。
然后我给它一个到php页面的URL链接,例如mail_my_form.php
然后我创建了一个php表单,并将其命名为与上面相同的... mail_my_form.php
最后一件事是在这个PHP代码的根目录中创建一个名为pdfs的文件夹。 (所以如果你将php放在一个名为email的文件夹中,那么在电子邮件文件夹中,你需要另一个名为pdfs的文件夹)
现在这个脚本的作用是:
将PDF保存为文件名pdfs。
然后它将文件附加到电子邮件并发送。
然后它从文件夹pdfs中删除该文件以节省空间。 (如果您愿意,也可以取出删除功能将表格保存在FTP上。
在这里。
<?php
$fileatt = date("d-m-Y-His") . ".pdf"; // Creates unique PDF name from the date
copy('php://input',"pdfs/".$fileatt); // Copies the pdf form data to a folder named pdfs
$fileatt = "pdfs/".$fileatt; // Path to the file gives the pdfs folder plus the unique file name we just assigned
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "Application Form_".$fileatt.".pdf"; // Filename that will be used for the file as the attachment when it is sent
$email_from = "mywebsite"; // Who the email is from
$email_subject = "Completed online Applications"; // The Subject of the email
$email_message = "Please find a recent online application attached.
";
$email_message .= "Any problems please email me...
"; // Message that the email has in it
$email_to = "youremail@yourserver.com"; // Who the email is to
$headers = "From: ".$email_from;
//no need to change anything else under this point
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
unlink($fileatt); //NOW WE DELETE THE FILE FROM THE FOLDER pdfs
Header("Location: nextpage.php"); //where do we go once the form has been submitted.
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?>
希望这有助于你们中的一些人。
理查德威廉姆斯