2014-06-10 4 views
0

서버에 이미지 파일을 업로드하고 싶습니다. & 데이터베이스에 이미지를 저장하고 싶습니다.codeigniter를 사용하여 이미지 파일을 업로드하는 방법은 무엇입니까?

데이터베이스에 이미지 파일을 저장하는 방법 &도 데이터베이스에서 이미지 파일을 가져올 수 있습니다. 이 문제를 해결하는 데 도움주세요.

프로젝트에서이 코드를 수행했습니다 ... 서버에서 업로드 할 때 upload_path를 찾을 수 없습니다. 컨트롤러 있음

$config['upload_path'] = base_url().'aplication/upload/'; // the uploaded images path 
     $config['allowed_types'] = 'jpg|jpeg|png';/*types of image extentions allowed to be uploaded*/ 
     $config['max_size'] = '3048';// maximum file size that can be uploaded (2MB) 
     $this->load->library('upload',$config); 

     if (! is_dir($config['upload_path'])) /* this checks to see if the file path is wrong or does not exist.*/ 
     { 
      echo($config['upload_path']); 
      die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path 
     $this->load->library('upload',$config); /* this loads codeigniters file upload library*/ 
     } 


     if (!$this->upload->do_upload()) 
     { 
      $error=array('error'=>$this->upload->display_errors()); 
      //echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error 
      // $this->load->view('main_view',$error); 
     } 
     else 
     { 
      $file_data=$this->upload->data(); 
      $data['img']=base_url().'.application/upload/'.$file_data['file_name']; 
      echo("Image upload successfully.."); 
//================================================ 

      // $this->load->model('insert_model'); 
      // $this->insert_model->submit_image(); 

//========================================== 
      //$this->insert_model->insert_images($this->upload->data()); 
      // $this->load->model('insert_model'); 
      //$this->insert_model->submit_image(); 


     } 
+0

[imageignoring with codeigniter] 중복 가능 (http://stackoverflow.com/questions/8942477/image-uploading-with-codeigniter) – Zoe

답변

0

매우 쉽습니다. 컨트롤러에 이미지 업로드 기능을 한 번만 만들면됩니다.

if ($_FILES['userfile']['name'] != "")//Check if file was selected by the user 
{ 
    $ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);//Get the extension of file 
    $picture_name = $this->image_upload("your/upload/directory/", 10000, 10000, 100000, "test", $ext); 
    //Now save this $picture_name in database 

} 
else 
{ 
    print_r($this->upload->display_errors());//Display errors if file does not get uploaded successfully 
} 

그게 전부를 다음과 같이하십시오 다음과 같이 파일 입력 필드 호출이 기능이 형태의 작용 기능에서 이제

function image_upload($path, $size, $width, $height, $new_name, $ext) 
{ 
    $config['upload_path'] = $path; 
    $config['allowed_types'] = 'jpg|png'; 
    $config['max_size'] = $size; 
    $config['file_name'] = time() . $new_name . "." . $ext; 
    $config['max_width'] = $width; 
    $config['max_height'] = $height; 
    $this->load->library('upload', $config); //Load codeigniter's file upload library 
    if (!$this->upload->do_upload()) 
    { 
     return false; 
    } else 
    { 
     return $config['file_name']; 
    } 
} 

: 여기에 내가 쓴 한 기능입니다.

0

This을 읽으면 파일을 업로드하는 방법이 표시됩니다.

이제 데이터베이스에 저장하는 방법! 컨트롤러 사용이에서

: 필요할 때마다 다시 그 경로를 얻을 수 있도록 경로로 upload_image($full_url) 기능 저장 이미지 이름을 사용하여 모델에서

if (! $this->upload->do_upload()) 
    { 
     $error = array('error' => $this->upload->display_errors()); 

     $this->load->view('upload_form', $error); 
    } 
    else 
       { 

         $img_data = array('upload_data' => $this->upload->data());       
         $full_url = "/uploads/".$img_data['upload_data']['file_name']; 
         $reply = $this->model_name->upload_image($full_url); 


       } 

.

관련 문제