使用codeigniter控制器上传图像

时间:2016-02-13 11:27:05

标签: php sql codeigniter

public function add_software()
{   
    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<div class="error-form">',   
'</div>');
    $this->form_validation->set_rules('name', 'Name', 
'required|min_length[4]|max_length[25]');
    $this->form_validation->set_rules('desc', 'Description', 'required');
    $this->form_validation->set_rules('licence', 'Licence.', 'required');
    $this->form_validation->set_rules('platform', 'Platform', 'required');
    $this->form_validation->set_rules('developers', 'Developer',     
'required');
    $this->form_validation->set_rules('link', 'Developer Website', 
'required');
    $this->form_validation->set_rules('cat', 'Category', 'required');
    $this->form_validation->set_rules('logo', 'Software Logo', 'required');

    if ($this->form_validation->run() == FALSE) {
    $this->load->view('admin/includes/header');
    $this->load->view('admin/add_software');
    $this->load->view('admin/includes/footer');
    } else {
    //Setting values for tabel columns
    $data['name']        = $this->input->post('name');
    $data['description'] = $this->input->post('desc');
    $data['licence']     = $this->input->post('licence');
    $data['platform']    = $this->input->post('platform');
    $data['developers']  = $this->input->post('developers');
    $data['link']        = $this->input->post('link');
    $data['category']    = $this->input->post('cat');
    $data['logo']    = $this->input->post('logo');

    /////////////////////////////////
    //set preferences

    $config = array(
    'upload_path' => '' .base_url(). '/assets/img/logo/',
    'allowed_types' => "gif|jpg|png|jpeg|pdf",
    'overwrite' => TRUE,
    'max_size' => "4096000", 
    'max_height' => "1024",
    'max_width' => "1024"
    );
            //load upload class library

    $this->load->library('upload', $config);
           $this->upload->data();
           $this->upload->do_upload();
    /////////////////////////////////
    //Transfering data to Model
    $this->insert_model->form_insert($data);
    $data['message'] = 'Data Inserted Successfully';
    //Loading View
    redirect('admin/view_softwares', 'refresh');
    }
}

这是我的codeigniter控制器功能。使用此功能,我可以将数据添加到数据库。但是,我无法上传图片。在upload_files中启用了php.ini即可。我试着整天解决这个问题,但一切都是徒劳的。 我没有在我的html表单中使用enctype,因为当我使用enctype时,我收到以下错误:

undefined Index filename

2 个答案:

答案 0 :(得分:1)

朋友我会给我上传图片的代码。这肯定有用。 你可以参考这个。没有疑问,请问,我会帮助你

public function uploadImage() {
        $this->load->helper(array('form', 'url'));  
        $config['upload_path'] = 'assets/images/b2bcategory';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '2024';
        $config['max_height'] = '1768';
        $config['width'] = 75;
        $config['height'] = 50;
        if (isset($_FILES['catimage']['name'])) {
            $filename = "-" . $_FILES['catimage']['name'];
            $config['file_name'] = substr(md5(time()), 0, 28) . $filename;
        }
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;
        $field_name = "catimage";
        $this->load->library('upload', $config);
        if ($this->input->post('selsub')) {
            if (!$this->upload->do_upload('catimage')) {
                //no file uploaded or failed upload
                $error = array('error' => $this->upload->display_errors());
            } else {
                $dat = array('upload_data' => $this->upload->data());
                $this->resize($dat['upload_data']['full_path'],           $dat['upload_data']['file_name']);
            }
            $ip = $_SERVER['REMOTE_ADDR'];
            if (empty($dat['upload_data']['file_name'])) {
                $catimage = '';
            } else {
                $catimage = $dat['upload_data']['file_name'];
            }
            $data = array(            
                'ctg_image' => $catimage,
                'ctg_dated' => time()
            );
            $this->b2bcategory_model->form_insert($data);

        }
    }

答案 1 :(得分:0)

你的道路是错的。

'upload_path' => '' .base_url(). '/assets/img/logo/',
  

注意:确保您的上传路径&#34; assets文件夹&#34;在主目录外面   应用文件夹。您还可能需要设置文件夹权限。

尝试

'upload_path' => FCPATH . 'assets/img/logo/' 

或只是

'upload_path' => './assets/img/logo/' 

上传成功后访问文件信息preferences

$image_info = $this->upload->data();

echo $image_info['file_name'];

或通过模型

$this->insert_model->form_insert($data,  $image_info = array());