wordpress 中带有 php 表单的附件文件 [英] A attachment file with a php form in wordpress

查看:23
本文介绍了wordpress 中带有 php 表单的附件文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 wordpress 中制作一个 php 表单.我想在此表单中附上一个记事本或 PDF 文件.我使用邮件功能在电子邮件中收到所有这些例如在身体变量中

I am making a php form in wordpress.I would like to attach a notepad or PDF file along this form.I am getting all this in email using mail function in a body variable for example

$body =" Attachment: $Attachmentfile";

我收到的不是文件,而是用户上传的文件名,而不是文件.我正在尝试通过电子邮件接收文件.实际上我也在学习如何做

Instead of file i am receiving file name that user uploading instead of file.I am trying to receive file in email.Actually i am learning too that how to do it.

我分配给 htm 文件标签的 php 代码是,

My php code for assigning to htm file tag is,

    if(trim($_POST['Attachmentfile']) === '') {
$AttachmentfileError = 'Please enter a Pdf, Notepad or Word file.';
$hasError = true;
} 
else 
{
if($_FILES["Attachmentfile"]["name"] != "")  
{  
$strFilesName = $_FILES["Attachmentfile"]["name"];  
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["Attachmentfile"]["tmp_name"])));  
$strHeader .= "--".$strSid."\n";  
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
$strHeader .= "Content-Transfer-Encoding: base64\n";  
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
$strHeader .= $strContent."\n\n";  
}
else
{
$Attachmentfile = trim($_POST['Attachmentfile']);
}
}

和我用于获取文件的 Html 代码,

and my Html code for getting file ,

<li class="left">
<label for="CV">Attachments :</label><span class="error">*</span><br>
<input class="txt" type="file" name="Attachmentfile" id="Attachmentfile" value="<?php if(isset($_POST['Attachmentfile']))   echo $_POST['Attachmentfile'];?>">
</li>

这是我的邮件功能代码,

and this is my mail function code,

$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
$headers .= "\r\n" . 'Content-type: text/html';
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;

你能帮忙修改我的代码以便在电子邮件中获取文件吗?谢谢朋友.

Can you help and modify my code in order to get file in email.Thanks friends.

推荐答案

function property_add() {
    $data = $_POST['data'];
    $data = array_map('trim',$data);
    extract($data);
    $to  = "reciever mail here ";
    $subject = "subject here";

    $message ='Message here';


    $attachments = array();  // initialize attachment array 
    $upload_dir = wp_upload_dir();  // look for this function in wordpress documentation at codex 
    $upload_dir = $upload_dir['path'];

    foreach ($_FILES["images"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {

            $tmp_name = $_FILES["images"]["tmp_name"][$key]; // Get temp name of uploaded file
            $name = time().'_'.$_FILES["images"]["name"][$key];  // rename it to whatever
            move_uploaded_file($tmp_name, "$upload_dir/$name"); // move file to new location 
            $attachments[] =  "$upload_dir/$name";  //  push the new uploaded file in attachment array
        }
    }

    add_filter( 'wp_mail_content_type', 'set_html_content_type' ); // allow html tags in mail
    if(wp_mail($to, $subject, $message,'',$attachments)) {
        echo "any success message>";
    } else {
        echo "failure message ";
    }
    remove_filter( 'wp_mail_content_type', 'set_html_content_type' );  // remove filter to avoid conflicts 

    if(!empty($attachments)) {
        foreach($attachments as $attachment) {
            @unlink($attachment); // delete files after sending them
        }
    }


} 

这是我的一个项目的摘录..根据您的需要更改它我写了评论,以便您变得更好..

this is an extract of one of my project .. change it according to your needs i wrote comment so that you get better better ..

在表单提交时调用这个函数

call this function on form submit

这篇关于wordpress 中带有 php 表单的附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆