2016-06-01 15 views

Antwort

0
$config['upload_path'] = 'uploads/images/'; 
$config['allowed_types'] = 'jpg|png'; 
$config['max_size'] = '2000'; 
$config['max_width'] = '1024'; 
$config['max_height'] = '768'; 
$this->load->library('upload', $config); 

$data = []; 
$errors = []; 
foreach ($this->input->post('files') as $file_name) 
{ 
    if ($this->upload->do_upload($file_name)) 
    { 
     $data[$file_name] = $this->upload->data(); 
    } 
    else 
    { 
     $errors[$file_name] = $this->upload->display_errors(); 
    } 
} 
0

Verwenden unten Funktion

function upload_image() 
{ 
$img1 = $this->_upload_image('Student_Image','YOUR_FOLDER_PATH','jpg|png|jpeg|gif',3000); 
if($img1['status']){$filename1 = $img1['filename'];} 
$img2 = $this->_upload_image('Father_Image','YOUR_FOLDER_PATH','jpg|png|jpeg|gif',3000); 
if($img2['status']){$filename2 = $img2['filename'];} 

$img3 = $this->_upload_image('Mother_Image','YOUR_FOLDER_PATH','jpg|png|jpeg|gif',3000); 

if($img3['status']){$filename3 = $img3['filename'];} 
} 

function _upload_image($userfile,$image_path,$allowed,$max_size) 
{ 
    if($_FILES[$userfile]['name']!='') 
    { 
     if(!is_dir($image_path)) 
     { 
      mkdir($image_path); 
     } 
     $config['upload_path'] = $image_path; 
     $config['allowed_types'] = $allowed; 
     $config['max_size'] = $max_size; 
     $img=$_FILES[$userfile]['name']; 
     $random_digit=rand(00,99999); //here you can change file name 
     $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1)); 
     $file_name=$random_digit.$ext; 
     $config['file_name'] = $file_name; 
     $this->ci->load->library('upload', $config); 

     if($this->ci->upload->do_upload($userfile)) 
     { 
      return array('status'=>TRUE,'filename'=>$this->ci->upload->file_name); 
     } 
     else {return array('status'=>FALSE,'error'=>$this->ci->upload->display_errors('<span>','</span>'));} 
    } 
} 
+0

ist es möglich, Code in einer einzigen Funktion zu schreiben. und wie man den Dateinamen in der Datenbank speichert, nach welchem ​​Namen das Bild gespeichert wurde. –

+0

Siehe aktualisierte Antwort. Wenn Sie es in derselben Funktion tun, dann müssen Sie es dreimal schreiben, damit ich eine Funktion gemacht habe. Diese Funktion gibt ein Array mit Dateiname zurück. – Vinie

0

müssen Sie mehr als 1 Bild zur Zeit hinzufügen, wenn Ihr Controller wie diese

public function upload_image($table_id){ 
     $this->data['table_id'] = $table_id; 
     $this->data['data'] = $this->your_model->get($table_id); 

     if($this->input->post()) { 
      $upload_image = true; 
      $upload_path = FCPATH.'/uploads/upload_image'; 
      $uploadedImageName = array(); 
      for ($i=1;$i<=5;$i++){ 
       $field_name ='image'.$i; 
       $temp_file_names = $this->file[$field_name]['name']; 
       if(isset($temp_file_names) && $temp_file_names!=''){ 
        $file_name = time().'_'.$this->randomString(10).'.'.$this->getFileExtension($temp_file_names); 
        if(!$this->uploadImage($upload_path,$file_name,$field_name)){ 
         $this->session->set_flashdata('error', $this->file_error); 
        } else { 
         $uploadedImageName[] = $this->file_data['file_name']; 
        } 
       } else { 
        $uploadedImageName[] = $this->input->post('old_image'.$i); 
       } 
      } 
      $update_data = array('Student_Image'=>json_encode($uploadedImageName)); 
      if(!$this->your_model->updateimage($table_id,$update_data)){ 
       $this->session->set_flashdata('error', 'Record couldn\'n updated. Please try again.'); 
      } else { 
       $this->session->set_flashdata('success', 'Update successfully.'); 
       redirect('/upload_image/'.$table_id); 
      } 
     } 
     $this->load->view('/upload_image',$this->data); 
    } 

und Modell sein sollte

public function updateimage($table_id,$data){ 
     $sql ="update tablename set "; 
     $update_data = array(); 
     if(empty($data)){ 
      return false; 
     } 
public function get($table_id){ 
     $sql ="select * from tablename where table_id = ?"; 
     $rs = $this->db->query($sql,array($table_id)); 
     $record = $rs->result(); 
     return (array)$record[0]; 
    } 
+0

Ich habe ein Formular Felder hinzugefügt können Sie erklären, wie Sie Daten einfügen und hochladen .. –

+0

lassen Sie mich wissen, wo Sie hinzufügen ..... – Harshita

0

können Sie versuchen, wie diese

 $errors= array(); 
     foreach($_FILES['gallery']['tmp_name'] as $key => $tmp_name){ 
      $file_name = $key.$_FILES['gallery']['name'][$key]; 
      $file_size =$_FILES['gallery']['size'][$key]; 
      $file_tmp =$_FILES['gallery']['tmp_name'][$key]; 
      $file_type=$_FILES['gallery']['type'][$key];  

      if($file_size > 2097152){ 
       $errors[]='File size must be less than 2 MB'; 
      }  

      $desired_dir="assets/images/products/"; 
      $array=array('pid'=>$insert_id,'imagepath'=>$desired_dir.$file_name); 
      if(empty($errors)==true){ 

        if(is_dir($desired_dir)==false){ 
         mkdir("$desired_dir", 0700);  // Create directory if it does not exist 
        } 
        if(is_dir("$desired_dir/".$file_name)==false){ 
         move_uploaded_file($file_tmp,"$desired_dir/".$file_name); 
       }else{         // rename the file if another one exist 
       $new_dir="$desired_dir/".$file_name.time(); 
       rename($file_tmp,$new_dir) ;    
       } 
       $this->db->insert('mx_products_images',$array);    
      } 
     } 
0
<?php echo form_open_multipart('upload_controller/do_upload');?> 
<div class="profilethumb"> 
<h4>Student's Image</h4> 
<input type="file" name="S_image" id="image" class="btn btn-primary"> 
</div><!--profilethumb--> 
<div class="profilethumb"> 
<h4>Father's Image</h4> 
<br><input type="file" name="F_image" class="btn-primary"><br> 
</div><!--profilethumb--> 
<div class="profilethumb"> 
<h4>Mother's Image</h4> 
<br><input type="file" name="M_image" class="btn-primary"><br> 
</div><!--profilethumb--> 
<div class="profilethumb"> 
<h4>Local Guardian Image</h4> 
<br><input type="file" name="LG_image" class="btn-primary"><br> 
</div><!--profilethumb--> 
<input type="submit" value="submit detail" class="btn-primary" /> 
<?php echo "</form>"?> 

Das ist meine Von Details ist .. Hochladen von Bildern in Upload-Ordner und Bilder in S_Info Tabelle in Datenbank-Namen speichern.