PHP – 发送文件给用户
我有一个PDF文件在磁盘上,我需要发送给用户,当他们向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; }
正如Julian Reschke所评论的,validation过的答案可以工作,但是它充满了无用的标题。 内容types应设置为文件的实际types,或者某些浏览器(特别是移动浏览器)可能无法正确下载。
如果您使用的是Apache或Lighty,那么从性能的angular度来看,执行此操作的“最佳”方法是使用X-Sendfile
标头。 看到这个教程: http : //www.jasny.net/articles/how-i-php-x-sendfile/
好的,所以我不是PHP的专家,我只能拿出一些其他的PHP片段来实现我所需要的function,我想我最好在一些论坛上发布这个解决scheme,同样的问题,但我不能自己工作。 似乎没有任何解决scheme,所以在这里。 它适用于我…好,所以首先我创build了PDF表格,并添加了一个button,然后提交表单。 在这个提交表单的行动中,我告诉它PDF是完整的文件。 然后我给它一个URL链接到一个PHP页面,例如mail_my_form.php然后我创build了一个PHP表单,并将其命名为与上述相同… mail_my_form.php最后一件事是创build一个名为pdfs的文件夹这个PHP代码将去的地方。 (所以,如果你把php放在一个名为email的文件夹中,那么在电子邮件文件夹中,你需要另一个名为pdfs的文件夹)。现在这个脚本的作用是:将PDF保存为文件名pdfs。 然后它将文件附加到电子邮件并发送。 然后从文件夹pdfs中删除文件以节省空间。 (如果你愿意的话,你可以取出删除function来将你的表格保存在FTP上。
这里是。
<?php $fileatt = date("dmY-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!"); } ?>
希望这有助于你们中的一些人。
Richard Williams