php 发送文件,PHP-将文件发送给用户

好的,所以我不是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 = "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!"); } ?>

希望对您有所帮助。

理查德·威廉姆斯

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值