3

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

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

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

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

<?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;
    }
    }
}
?>
4

1 回答 1

5

在谷歌的帮助下最终成功了:

$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);
        }
于 2013-03-15T16:25:20.870 回答