微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Codeigniter – 将2个文件附加到已从用户表单上传到网络服务器的电子邮件中

我有一个表格,填写时上传2个文件(一个是doc,docx或pdf,另一个是图像文件).我的控制器验证表单ok并将文件上传到服务器.我的问题似乎是访问上传文件的数组!?

我在表单中引用了一个名为$uploaded的变量中的数组,并且从错误中看出实际的数组似乎被称为“成功”(我压缩它从upload.PHP获取名称)我认为这包含2个称为[0]和[1]的元素,每个元素包含另一个文件属性数组.

基本上我试图将这两个文件附加到电子邮件中.我希望有人能帮助新手吗?我将下面的控制器代码,如果您需要任何其他代码,请告诉我.

编辑:我已经更新了以下代码
我现在已经取得了一些成功(不多):for循环现在附加了一个文件,但是两次.所以要么我的代码没有正确索引数组,要么数组没有存储两个文件信息?

<?PHP
class Form extends CI_Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('first_name', 'First Name', 'required|alpha');
        $this->form_validation->set_rules('last_name', 'Surname', 'required|alpha');
        $this->form_validation->set_rules('dob', 'Date of Birth', 'required');
        $this->form_validation->set_rules('nationality', 'Nationality', 'required|alpha');
        $this->form_validation->set_rules('gender', 'Gender', 'required');
        $this->form_validation->set_rules('address_l1', 'Address Line 1', 'required|alpha_dash_space');
        $this->form_validation->set_rules('address_l2', 'Address Line 2', 'alpha');
        $this->form_validation->set_rules('address_city', 'City', 'required|alpha');
        $this->form_validation->set_rules('address_postcode', 'Post Code', 'required|alpha_dash_space');
        $this->form_validation->set_rules('address_country', 'Country', 'required|alpha_dash_space');
        $this->form_validation->set_rules('e_address', 'Email Address', 'required|valid_email');
        $this->form_validation->set_rules('h_tel', 'Home Telephone Number', 'required|numeric');
        $this->form_validation->set_rules('mobile', 'Mobile Number', 'required|numeric');
        $this->form_validation->set_rules('university', 'University', 'required|alpha_dash_space');
        $this->form_validation->set_rules('campus', 'Campus Name', 'required|alpha_dash_space');
        $this->form_validation->set_rules('course', 'Course Title', 'required|alpha_dash_space');
        $this->form_validation->set_rules('end_date', 'Course End Date', 'required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('home');
        }
        else
        {

            //display Success page
            $this->load->view('formsuccess');

            //Send Email
            $this->load->library('email');

            //Array helper
            $this->load->helper('array');

            //Upload files to server
            $this->load->library('upload');

            $config['upload_path']   = './attachments'; //if the files does not exist it'll be created
            $config['allowed_types'] = 'gif|jpg|png|doc|docx|txt|pdf';
            $config['max_size']   = '4000'; //size in kilobytes
            $config['encrypt_name']  = TRUE;

            $this->upload->initialize($config);

            $uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.PHP files

            $data = $this->upload->data();          

            for ($i = 0; $i <= 1; $i++) {
                // ignore file that have not been uploaded
                //if (empty($_FILES['uploaded'.$i])) continue;

                //get the data of the file
                //$fileName = $data['uploaded'.$i]['name'];
                $filepath = $data['full_path'];
                //add only if the file is an upload
                echo $data['full_path'];
                $this->email->attach($filepath); //$mail->AddAttachment($filePath, $fileName);
            }


            $this->email->from('your@example.com', 'Your Name');
            $this->email->to('t.bryson@shu.ac.uk'); 

            $this->email->subject('Application for Accommodation (Non-SHU)');
            $this->email->message('Testing the email class.');  

            $this->email->send();

            echo $this->email->print_debugger();
        }
    }
    function alpha_dash_space($str_in)
    {
    if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in)) {
    $this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
    return FALSE;
    } else {
    return TRUE;
    }
    }
}
?>

解决方法:

在谷歌的一些帮助下,它最终工作了:

$uploaded = $this->upload->up(FALSE); //Pass true if you want to create the index.PHP files
        var_dump($uploaded); 
        $data = array('uploaded_data');

        //Attach the 2 files to email
        foreach($_FILES as $key => $value){
            //var_dump($uploaded['success'][$key]['full_path']); //FOR TESTING
            $file = $uploaded['success'][$key]['full_path'];
            $this->email->attach($file);
            //unlink($file);
        }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐