上传时重命名文件

时间:2017-09-19 11:35:24

标签: codeigniter file-upload rename

我想在上传此文件时重命名文件。 所以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);
                    }
            }

1 个答案:

答案 0 :(得分: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;
    }