2017-09-19 7 views
0

Ich möchte eine Datei umbenennen, wenn ich diese Datei hochlade. Also der Inhalt der add_land_short sollte den Namen der Datei erhalten. Also wenn der Eingabetest ist, ist die Datei test.png. ControllerDatei beim Hochladen umbenennen

:

 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/'); 
      } 

Modell:

  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); 
        } 
      } 

Antwort

0

Probieren Sie diese

$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; 
    } 
Verwandte Themen