2013-02-26 51 views
3

我有當填寫上傳2個文件(一個是DOC,DOCX,或pdf,而另一個是一個圖像文件)形式的電子郵件。我的控制器驗證窗體確定並將文件上傳到服務器。我的問題似乎是訪問上傳的文件數組!!笨 - 2個文件附加到已經從用戶表格上傳到網絡服務器

我已經在表單中引用了一個名爲$ uploaded的變量中的數組,並從它出現的錯誤中看出實際數組似乎被稱爲'success'(我按下它從upload.php庫中獲取該名稱)I認爲這於是包含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('[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; 
    } 
    } 
} 
?> 
+0

什麼是您的var_dump($ _ FILES);看起來像? – jco 2013-02-26 14:53:39

+0

這是var_dump($ _ files)的數據:array(size = 2) 0 => array(size = 6) 'name'=> string'附錄1 Final Structures.docx'(length = 32) '類型'=>字符串 '應用/ vnd.openxmlformats-officedocument.wordprocessingml.document'(長度= 71) 'tmp_name的值'=>字符串 'C:\瓦帕\ TMP \ php97.tmp'(長度= 21) '錯誤」 => INT 0 '尺寸'=> INT 779606 'file_input'=>字符串 'file_1'(長度= 6) 1 => 陣列(大小= 6)..... – 2013-02-26 16:08:38

回答

5

曾爲它outeventually,從谷歌一些幫助:

$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); 
     } 
相關問題