3

記入すると2つのファイルをアップロードするフォームがあります(1つはdoc、docx、またはpdfで、もう1つは画像ファイルです)。私のコントローラーはフォームOKを検証し、ファイルをサーバーにアップロードします。私の問題は、アップロードされたファイルの配列にアクセスしているようです!?

フォームで$uploadedという変数で配列を参照しましたが、エラーから、実際の配列は「success」と呼ばれているようです(upload.phpライブラリからこの名前を取得していると思います)。 [0]および[1]と呼ばれる2つの要素。それぞれに、ファイル属性の別の配列が含まれています。

私は基本的にこれらの2つのファイルを電子メールに添付しようと円を描いて回っています。誰かが初心者を助けてくれることを願っていますか?以下にコントローラーコードを配置します。他のコードが必要な場合はお知らせください。

編集:以下のコードを更新しました。少し成功しました(あまり成功していません):forループはファイルの1つを添付していますが、2回添付しています。それで、私のコードが配列に正しくインデックスを付けていないか、配列が両方のファイル情報を保存していないのですか?

<?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 に答える