2017-09-19 5 views
0

이 파일을 업로드 할 때 파일 이름을 변경하고 싶습니다. add_land_short의 내용은 파일의 이름을 가져야합니다. 그렇다면 입력 테스트는 test.png 파일입니다.업로드 할 때 파일 이름 바꾸기

컨트롤러 :

 public function insert_land() 
      { 
       $land_short = $this->input->post('add_land_short'); 
       $config['upload_path'] = './assets/images/site/flag/'; 
       $config['allowed_types'] = '*'; 
       $config['max_size'] = '2048'; 
       $config['max_width'] = '2000'; 
       $config['max_height'] = '2000'; 
       $this->load->library('upload', $config); 
       if (!$this->upload->do_upload()) 
        { 
         $errors = array('error' => $this->upload->display_errors()); 
         $post_image = 'noimage.jpg'; 
        } 
       else 
        { 
         $data = array('upload_data' => $this->upload->data()); 
         $post_image = $_FILES['userfile']['name']; 
        } 
       $this->Admin_model->insert_land(); 
       redirect('admin/land/'); 
      } 

모델 :

  public function insert_land() 
      { 
       $this->db->where('tb_land_name', $this->input->post('add_land_name')); 
       $this->db->where('tb_land_kurzel', $this->input->post('add_land_short')); 
       $this->db->where('tb_land_img', $this->input->post('add_land_short')); 
       $result = $this->db->get('db_land'); 
       if($result->num_rows() < 1) 
        { 
         $data = array( 'tb_land_name' => $this->input->post('add_land_name'), 
             'tb_land_kurzel' => $this->input->post('add_land_short'), 
             'tb_land_last_buy' => date('Y-m-d'), 
             'tb_land_img' => $this->input->post('add_land_short'), 
             ); 
         return $this->db->insert('db_land', $data); 
        } 
      } 

답변

0

$file_name = time() . rand(0, 9999) . "." . pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION); 
    $land_short = $this->input->post('add_land_short'); 
    $config['upload_path'] = FCPATH . 'assets/images/site/flag/'; 
    $config['allowed_types'] = '*'; 
    $config['max_size'] = '2048'; 
    $config['max_width'] = '2000'; 
    $config['max_height'] = '2000'; 
    $config['file_name'] = $file_name; 
    $this->load->library('upload', $config); 
    if (!$this->upload->do_upload()) { 
     $errors = array('error' => $this->upload->display_errors()); 
     $post_image = 'noimage.jpg'; 
     redirect('admin/land/'); 
    } else { 
     $data = array('upload_data' => $this->upload->data()); 
     $post_image = $file_name; 
    } 
시도
관련 문제