2017-04-20 4 views
0

이미지를 루트 폴더에 저장하고 파일 이름을 데이터베이스에 업로드하려고합니다. 업로드 기능을 위해 내가 한 일이 여기에 있습니다.codeigniter에서 이미지를 업로드하는 방법은 무엇입니까?

 public function add_blog($id=0){ 
     if(!empty($_FILES['picture']['name'])){ 
      $config['upload_path'] = 'uploads/blog_image'; 
      $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
      $config['file_name'] = $_FILES['picture']['name']; 

      //Load upload library and initialize configuration 
      $this->load->library('upload',$config); 
      $this->upload->initialize($config); 
      // print_r($value['name'][$s]);exit; 
      if($this->upload->do_upload('picture')){ 
       $uploadData = $this->upload->data(); 
       $picture = $uploadData['file_name']; 
        print_r($picture); exit; 
      } 
      } 
      print_r($config['file_name']); exit; 


    $data['blog_data']=array('blog_post'=>$this->input->post('blog_post'), 
          'posted_by'=>$this->input->post('posted_by'), 
          'blog_image'=>$picture); 

if ($id==0){ 
     $this->db->insert('blog',$data['blog_data']); 
     // $last_id = $this->db->insert_id(); 

     } 
    else { 
    $this->db->where('id',$id); 
    // $last_id = $this->db->insert_id(); 
    $this->db->update('blog',$data['blog_data']); 

} 
} 

여기의 문제는 이미지를 제외한 다른 데이터를 삽입 할 수 있다는 것입니다. print_r ($ config [file_name])으로 print_r()을 사용하고 종료하면 이미지 이름을 얻습니다. 그렇지 않으면 이미지를 제외한 다른 데이터를 삽입합니다. 그러나 이미지는 루트 폴더 나 데이터베이스의 이름으로 업로드되지 않습니다. 기존의 업로드 경로를 제공하지 않으면 오류도 발생하지 않습니다. 나는 내부 코드가 실행되지 않는다고 생각한다. 어떻게 해결할 수 있을까요? 미리 감사드립니다.

당신은 $ _FILES & & $ _FILES [ '사진'] [ '이름'] 사용할 필요가 없습니다

답변

1
private function _upload_image() { 
    $this->load->library('upload'); 
    if ($_FILES && $_FILES['picture']['name'] !== ""){ 
     $config['upload_path'] = 'uploads/blog_image'; 
     $config['allowed_types'] = 'jpg|jpeg|png|bmp'; 
     $config['max_size']  = 10000; 
     /*the picture name must be unique, use function now()*/ 
     $config['file_name']  = $_FILES['picture']['name'] . now(); 
     $config['file_ext_tolower']  = TRUE; 
     $this->upload->initialize($config); 
     if ($this->upload->do_upload('picture')){ 
      $file_name = $this->upload->data()['file_name']; 
      $full_path = $this->upload->data()['full_path']; 
      /*If you want create a thumb, use this part*/ 
      $this->load->library('image_lib'); 
      $config = array(
       'source_image'  => $path, 
       'new_image'   => $this->_image_path, 
       'maintain_ratio' => true, 
       'width'    => 128, 
       'height'   => 128, 
       'create_thumb'  => TRUE, 
       'thumb_marker'  => '_thumb', 
      ); 
      $this->image_lib->initialize($config); 
      $this->image_lib->resize(); 
      /*Save in database*/ 
      $this->db->insert('blog', [ 
       'file_name' => $file_name, 
       'full_path' => $full_path 
      ]); 
     } else { 
      //if picture is empty, do something 
     } 
    } 
} 

! $ this-> upload-> do_upload ('picture')와 $ this-> upload-> data()에서 데이터를 가져 오는 경우에만

을 읽으십시오.
public function add_blog() 
    { 
      $config['upload_path']   = '.uploads/blog_image'; 
      $config['allowed_types']  = 'jpg|jpeg|png|gif'; 
      $config['max_size']    = 10000; 

      $this->load->library('upload', $config); 

      if (! $this->upload->do_upload('picture')) 
      {//Do something with errors 
        $errors = $this->upload->display_errors(); 
      } 
      else 
      { 
        $data = $this->upload->data(); 
        $this->db->insert('blog', [ 
          'file_name' => $data['file_name'], 
          'full_path' => $data['full_path'] 
        ]); 
      } 
    } 
+0

약간 간단한 방법으로 할 수 있습니까? –

+0

예, 게시물을 업데이트합니다. –

0

업로드 할 파일 크기를 언급하지 않았습니다. 위의 코드에서이 작업을 수행했습니다. 편집

public function add_blog($id=0){ 
     if(!empty($_FILES['picture']['name'])){ 
      $config['upload_path'] = 'uploads/blog_image'; 
      $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
      $config['max_size']    = 0; 
      $config['file_name'] = $_FILES['picture']['name']; 

      //Load upload library and initialize configuration 
      $this->load->library('upload',$config); 
      $this->upload->initialize($config); 
      // print_r($value['name'][$s]);exit; 
      if($this->upload->do_upload('picture')){ 
       $uploadData = $this->upload->data(); 
       $picture = $uploadData['file_name']; 
        // print_r($picture); exit; 
      } 
      } 
      // print_r($config['file_name']); exit; 


$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'), 
          'posted_by'=>$this->input->post('posted_by'), 
          'blog_image'=>$picture); 

if ($id==0){ 
     $this->db->insert('blog',$data['blog_data']); 
     // $last_id = $this->db->insert_id(); 

} 
else { 
    $this->db->where('id',$id); 
    // $last_id = $this->db->insert_id(); 
    $this->db->update('blog',$data['blog_data']); 

} 
} 

그리고이 코드 삽입 및 업데이트에 모두 사용할 수 있습니다.

관련 문제