带有文件附件的PHP表单发送到邮件
我有一个PHP表单脚本发送到邮件与文件附件,但它没有一个功能,以有效的文件大小和文件扩展名 下面是php代码带有文件附件的PHP表单发送到邮件,php,forms,email,attachment,Php,Forms,Email,Attachment,我有一个PHP表单脚本发送到邮件与文件附件,但它没有一个功能,以有效的文件大小和文件扩展名 下面是php代码 ### Attachment Preparation ### $file_attached = false; if(isset($_FILES['file_attach'])) //check uploaded file { //get file details we need $file_tmp_name = $_FILES['file_attach']['tm
### Attachment Preparation ###
$file_attached = false;
if(isset($_FILES['file_attach'])) //check uploaded file
{
//get file details we need
$file_tmp_name = $_FILES['file_attach']['tmp_name'];
$file_name = $_FILES['file_attach']['name'];
$file_size = $_FILES['file_attach']['size'];
$file_type = $_FILES['file_attach']['type'];
$file_error = $_FILES['file_attach']['error'];
//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
//now we know we have the file for attachment, set $file_attached to true
$file_attached = true;
}
if($file_attached) //continue if we have the file
{
# Mail headers should work with most clients
$headers = "MIME-Version: 1.0\r\n";
$headers = "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From: ".$from_email."\r\n";
$headers .= "Subject: ".$subject."\r\n";
$headers .= "Reply-To: ".$user_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."\r\n\r\n";
$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n\r\n";
$headers .= $message_body."\r\n\r\n";
这是php表单代码
<label>
<span>Attachment</span>
<input type="file" name="file_attach" class="input-field" />
</label>
<label>
<span> </span><input type="submit" id="submit_btn" value="Submit" />
</label>
附件
我不想使用jQuery,我想用php来实现它。您将使用类似这样的方法来验证文件扩展名:
if($_FILES["file_attach"]["type"] == "image/jpeg") { //...
将“image/jpeg”替换为$\u FILE['FILE\u attach']['type']
值所对应的实际文件类型
至于验证大小,您将执行以下操作:
if($_FILE["file_attach"]["size"] > 1000000) { //...
最后,这可能是将其应用到现有脚本中的最佳方法:
//[...]
$file_error = $_FILES['file_attach']['error'];
if($_FILES["file"]["type"] != "image/jpeg"){
$file_error = 7;
}elseif($_FILES["file_attach"]["size"] > 1000000) { // = 1MB
$file_error = 8;
}
//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder"
7=>"We do not accept files of that type.",
8=>"Files cannot be larger than 1MB" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
//[...]
感谢您的回答,但我还面临一个问题,比如我将扩展名设置为(jpeg)的错误,当我尝试上载任何文件时,它的“仅此文本显示错误”(null),您是否看到我添加到
$mymsg
数组中的7和8的值?如果您不能更具体地说明错误,我无法帮助您-这是我的错误,有一些额外的点等,现在它工作得很好,当我尝试上传额外大小的文件时,它显示我错误,或者当我尝试上传无效的文件扩展名时,它显示我错误。现在我只想知道一件事,我需要把文件扩展名放在哪里?我想只允许像(jpg,gif,png)这样的图像文件,我需要把这些扩展名放在哪里,还有什么格式?