Tom Bryson
Tom Bryson

Reputation: 91

Codeigniter - attaching 2 files to email that have been uploaded from user form to webserver

I have a form which when filled out uploads 2 files (one is a doc,docx, or pdf and the other is an image file). My controller validates the form ok and uploads the files to the server. My problem seems to be accessing the array of the files uploaded!?

I've referenced the array in a variable called $uploaded in the form and from the error it appears the actual array seems to be called 'success' (I pressume it gets this name from the upload.php library) I think this then contains 2 elements called [0] and [1] which each contain another array of the file attributes.

I'm going round in circles basically trying to attach these two files to an email. I hope someone will be able to help a novice out? I will place the controller code below, if you need any other code please let me know.

EDIT: I have updated the code below I've now had a bit of success (not much): The for loop is now attaching 1 of the files, but twice. So either my code is not indexing the array correctly or the array is not storing both file information?

<?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('[email protected]', 'Your Name');
            $this->email->to('[email protected]'); 

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

Upvotes: 3

Views: 5839

Answers (1)

Tom Bryson
Tom Bryson

Reputation: 91

Worked it outeventually, with some help from google:

$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);
        }

Upvotes: 5

Related Questions