PHP - 将文件发送给用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2882472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - send file to user
提问by Ian morgan
I have a pdf file on disk that i need to send to a user when they make a request to a php script, what is the best way of doing this?
我在磁盘上有一个 pdf 文件,当他们向 php 脚本发出请求时,我需要将其发送给用户,这样做的最佳方法是什么?
Thanks
谢谢
回答by Adirael
Assuming that it's on the server:
假设它在服务器上:
readfile() — Outputs a file
readfile() — 输出文件
Example from http://php.net/manual/en/function.readfile.php
回答by Rayjax
Here is what you need to send a file with PHP :
以下是使用 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;
}
As Julian Reschke commented, the validated answer MAY work, but it's full of useless headers. The content-type should be set to the real type of the file, or some browsers (especially mobile browsers) may not download it properly.
正如 Julian Reschke 评论的那样,经过验证的答案可能有效,但它充满了无用的标题。content-type 应设置为文件的真实类型,否则某些浏览器(尤其是移动浏览器)可能无法正确下载。
回答by chiborg
If you are using Apache or Lighty, then the "best" way to do this from a performance point of view, is to use the X-Sendfileheader. See this tutorial: http://www.jasny.net/articles/how-i-php-x-sendfile/
如果您使用的是 Apache 或 Lighty,那么从性能的角度来看,执行此操作的“最佳”方法是使用X-Sendfile标头。请参阅本教程:http: //www.jasny.net/articles/how-i-php-x-sendfile/
回答by Richard
Ok, so I'm no expert at PHP, I can only take credit for putting together a few other snippets of PHP to achieve what i needed it to do, and i thought i had better post this solution up in a few forums which asked the same question but i could not get to work myself. There did not seem to be a solution anywhere so here it is. It works for me...
Ok so first off i created the PDF form and added a button which then submits form. In the actions of this submit form, i told it to PDF the complete document.
Then i gave it a URL link to a php page, such as mail_my_form.php
Then i created a php form, and named it the same as above... mail_my_form.php
One last thing is to create a folder called pdfs in the root of where this php code will go. (So if you put the php in a folder called email, then inside the folder of email, you need another folder called pdfs)
Now what this script does is:
Saves the PDF to the file name pdfs.
Then it attaches the file to an email and sends it.
Then it deletes the file from the folder pdfs to save space. (you could take out the delete function to save your forms on your FTP also if you wanted to.
Here it is.
好的,所以我不是 PHP 方面的专家,我只能将其他一些 PHP 片段放在一起来实现我需要它做的事情,我想我最好在几个论坛上发布这个解决方案同样的问题,但我无法自己工作。似乎在任何地方都没有解决方案,所以就在这里。它对我有用......好吧,首先我创建了 PDF 表单并添加了一个按钮,然后提交表单。在此提交表单的操作中,我告诉它 PDF 完整的文档。然后我给了它一个指向 php 页面的 URL 链接,例如 mail_my_form.php 然后我创建了一个 php 表单,并将其命名为与上面相同... mail_my_form.php 最后一件事是在根目录中创建一个名为 pdfs 的文件夹这个 php 代码的去向。(所以如果你把php放在一个叫做email的文件夹里,然后在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 = "[email protected]"; // 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!");
}
?>
Hope this helps some of you.
希望这对你们中的一些人有所帮助。
Richard Williams
理查德·威廉姆斯

