2013-10-07 109 views
2

我已經在codeigniter上工作多次上傳,但我有一個問題,我如何將這些文件的文件名插入到兩個表(文檔和圖像表)這兩個表有兩個相同的列名(ID,名稱)。有沒有辦法,我可以disjunct或隔離我的代碼上傳圖像和文檔。因爲我將它們合併爲一個功能。我有一個工作多個上傳(文檔和圖像)CODEIGNITER

這是我的代碼。這是工作。

VIEW

<?php echo form_open_multipart('test'); ?> 
    <label>Images</label> 
     <input type='file' multiple='multiple' name='userfile[]'> 
    <label>Documents</label> 
     <input type='file' multiple='multiple' name='userfile[]'> 
<?php echo form_submit('submit', 'Upload them files!') ?> 

控制器

function index() 
{ 
    if (isset($_POST['submit'])) 
    { 
     $this->load->library('upload'); 
     //$this->uploadfile($_FILES['userfile']); 
     $files = $_FILES; 
     $cpt = count($_FILES['userfile']['name']); 
     for($i=0; $i<$cpt; $i++) 
     { 

       $filename = $_FILES['userfile']['name']= $files['userfile']['name'][$i]; 
       $_FILES['userfile']['type']= $files['userfile']['type'][$i]; 
       $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; 
       $_FILES['userfile']['error']= $files['userfile']['error'][$i]; 
       $_FILES['userfile']['size']= $files['userfile']['size'][$i]; 


       $this->upload->initialize($this->set_upload_options()); 
       $this->upload->do_upload(); 
       $this->upload->data(); 
     } 
    } 
$this->load->view("test"); 
} 
private function set_upload_options() 
{ 
// upload an image and document options 
    $config = array(); 
    $config['upload_path'] = './upload_documents/'; 
    $config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG|pdf|doc|docx|xls|xlsx'; 
    $config['max_size'] = '0'; // 0 = no file size limit 
    $config['max_width'] = '0'; 
    $config['max_height'] = '0'; 
    $config['overwrite'] = TRUE; 


    return $config; 
} 

這些代碼工作,它能夠將所有文件傳輸所需的我的道路上。 但我想知道如何製作MODEL,當我在上面看到我的代碼時,如何識別文件類型我在變量「$ filename」上傳遞了文件的名稱。如果你使用print_r($ filename),你會看到所有的文件名和它的文件擴展名。這些名稱是我將根據它們的文件類型插入到我的兩個表中的名稱。

是否有CodeIgniter或PHP代碼的任何代碼,我將用它來識別文件類型並將其傳遞給帶有兩個函數(如upload_image或upload_docu)的模型?請幫助。

+1

我已經知道答案。 :) – Vincent

+0

您可以發佈答案,以便將來有人可以使用它。 – AlphaMale

+1

確定:)我會發布它。我希望我會有一個投票:) – Vincent

回答

2

所以這裏是..

控制器

function index() 
{ 
    if (isset($_POST['submit'])) 
    { 
     $this->load->library('upload'); 
     //$this->uploadfile($_FILES['userfile']); 
     $files = $_FILES; 
     $cpt = count($_FILES['userfile']['name']); 
     for($i=0; $i<$cpt; $i++) 
     { 

       $_FILES['userfile']['name']= $files['userfile']['name'][$i]; 
       $_FILES['userfile']['type']= $files['userfile']['type'][$i]; 
       $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; 
       $_FILES['userfile']['error']= $files['userfile']['error'][$i]; 
       $_FILES['userfile']['size']= $files['userfile']['size'][$i]; 


       $this->upload->initialize($this->set_upload_options()); 
       $this->upload->do_upload(); 
       $this->upload->data(); 
       $ext = pathinfo($filename, PATHINFO_EXTENSION); 
       $img_ext_chk = array('jpg','png','gif','jpeg','JPG','PNG', 'GIF', 'JPEG'); 
       if (in_array($ext,$img_ext_chk)) 
       { 
        $this->asset->add_image($filename);        
       } 
       else 
       { 
        $this->asset->add_document($filename); 
       } 
     } 
    } 
} 

,併到模型

public function add_image($filename) 
{ 
$data = array ('images' => $filename); 
$this->db->insert('asset_images', $data); 
} 

public function add_document($filename) 
{ 
$data = array ('documents' => $filename); 
$this->db->insert('asset_documents', $data); 
} 
相關問題