2012-03-16 2 views
3

서버의 폴더에 파일을 업로드하려면 codeIgniter를 사용하고 있습니다. 이제 데이터를 데이터베이스에 저장하고 싶습니다. 원시 데이터를 가져 와서 데이터베이스에 저장하려면 무엇이 필요합니까?코드 이그니 터 - 이미지를 데이터베이스에 저장하는 방법

$filename = $data['upload_data']['file_name']; 
$file_data = file_get_contents($data['upload_data']['file_name']); 

$this->load->model('files'); 
$this->files->saves($filename, $file_data, 'ID1'); 

테이블 구조 :

  • file_data (LONG BLOB)
  • 다음
    <?php 
    class files extends ci_Model{ 
        function saves($filename, $filedata, $post_id){ 
         $this->db->query("INSERT INTO tbl_files SET file_data='$filedata', filename='$filename', postid='$post_id'"); 
        } 
    } 
    ?> 
    

    내가 업로드 컨트롤러에서 호출하는 방법은 다음과 같습니다 여기

    는 파일 저장을위한 모델이다
  • 파일 이름 (VARCHAR)
+2

은 왜 그냥 이미지가 아닌 이미지 자체의 경로를 저장하지? 성능이 더 좋아질 것입니다. 보십시오 http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – Rooneyl

+0

그들은 데이터베이스에 저장할 때 더 안전하다고 말합니다. 게다가 휴대용이기 때문에 더 이상 사용할 수 없습니다. 하나의 운영체제에서 다른 운영체제로 변경할 때 파일 시스템 구조에 대해 걱정하십시오. –

+0

데이터베이스에 저장할 때 어떤 일이 쉽게 잘못 될 수 있기 때문에 제가하지 않을 것입니다. 보안을 돕기 위해 htdocs 폴더 위에 이미지를 저장할 수 있습니다 – Rooneyl

답변

-1

는이 같은 편리한는 mysql_real_escape_string()를 사용하여 문제를 해결 한 :

$filename = $data['upload_data']['file_name']; 
$file_data = mysql_real_escape_string(file_get_contents($data['upload_data']['full_path'])); 
1

그것을 시도, 그것은 일 : 당신이 시도하려는 경우 는하지만 ....

그것은 PHP 또는 CodeIgniter의에서와 동일

더 나은 내가 여기에 코드를 작성하는 당신에게 링크를 제공합니다 내 code`

function _save($id = null){ 

    $config['upload_path'] = './asset/'; 
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $config['max_size'] = '1000'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 
    $config['encrypt_name'] = TRUE; 
    $this->load->library('upload', $config);  

     if($_POST || $_FILES){ 
      if(!$this->upload->do_upload('field_input_name')){ 
       echo $this->upload->display_errors('<p>', '</p>'); 
      } else { 
       $w = $this->upload->data(); 
       $data = array(
        'field_input_name' => $w['file_name'], 
        ); 
       $this->db->insert('table_image', $data); 
      } 
     } 

}에

관련 문제