无法使用codeigniter的upload()函数上传图片

时间:2016-03-10 13:44:55

标签: php html css codeigniter file-upload

我创建了一个页面控制器,它调用视图来上传图像。页面控制器如下: 页面控制器:

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Pages extends MY_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('pages_model');
    }
    public function index(){
        if(!$this->session->userdata('logged_in')){
            redirect('admin');   
        }
        else{
        $data[] = '';
        $this->load->view('layouts/header');
        $this->load->view('Admin_views/Index_views/pages_view');
        $this->load->view('layouts/footer');
        }
    }
    public function create_page(){
        if(!$this->session->userdata('logged_in')){
            redirect('admin');   
        }
        else{
        $data[] = '';
        $this->load->view('layouts/header');
        $this->load->view('Admin_views/Page_views/create_page_view');
        $this->load->view('layouts/footer');
}
    }
    public function insert_page(){
        $this->form_validation->set_rules('page_name','Page Name','trim|required');
        $this->form_validation->set_rules('page_desc','Page Description','trim');
                $config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 2048000;
                $config['max_width']            = 1024;
                $config['max_height']           = 768;
                $this->load->library('upload', $config);

        if($this->form_validation->run()== FALSE){
            $errors = array(
                'errors'=>  validation_errors(),

            );
            $this->session->flashdata($errors);
            redirect('admin/pages/create_page');
        }
        else{
            $this->upload->do_upload('userfile');
             $data = $this->upload->data();
             $insert_data = array(
                 'page_topic'=>  $this->input->post('page_name'),
                 'page_desc'=>  $this->input->post('page_desc'),
                 'image_url'=> $data['full_path']
             );
             $result = $this->pages_model->insert_page($insert_data);
             if(!$result){

             }
             else{
                 redirect('admin/pages');  
             }
        }
    }
}

我的观点如下:

<div class="col-xs-9">
    <div class="panel panel-danger">
        <div class="panel-heading">
            <h3>Add New</h3>    
        </div>
        <div class="panel-body">
            <?php 
            $attributes = array(
                'class'=>'form-group',
                'name'=>'create_page'
            );
            echo form_open_multipart('admin/pages/insert_page',$attributes);?>
            <div class="form-group">
                <?php echo form_label('New Page Name');?>
                <?php $data = array(
                    'class'=>'form-control',
                    'name'=>'page_name',
                    'placeholder'=>'Enter Page Name'
                );
                echo form_input($data);
                ?></div>
            <div class="form-group">
            <?php echo form_label('Enter Page Description');?></div>
                <?php $data = array(
                    'class'=>'form-control',
                    'id'=>'mytextarea',
                    'name'=>'page_desc'
                );
                echo form_textarea($data);
                ?>
            <br>
            <div class="form-group">
            <?php echo form_label('Upload An Image');?></div>
            <div style="position:relative;">
        <input type="file" name="userfile" size="20" />
        <br>
            <input type="submit" value="Submit" class="btn btn-primary"/>

                <?php echo form_close();?>

        </div>
    </div>
</div>
<div class="col-xs-3">

</div>

完成所有这些后,其他数据如页面名称和描述将被上传到数据库中,但上传的上传文件夹中的图像和图像URL都没有上传到数据库中。我甚至没有收到错误。

顺便说一下,我正在使用wamp服务器在Windows平台上开发这个应用程序。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

请检查代码,了解错误信息

public function insert_page(){
    $this->form_validation->set_rules('page_name','Page Name','trim|required');
    $this->form_validation->set_rules('page_desc','Page Description','trim');
            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 2048000;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;
            $this->load->library('upload', $config);

    if($this->form_validation->run()== FALSE){
        $errors = array(
            'errors'=>  validation_errors(),

        );
        $this->session->flashdata($errors);
        redirect('admin/pages/create_page');
    }
    else{
        if ( ! $this->upload->do_upload("userfile"))
        {
            $error = array('error' => $this->upload->display_errors());
            print_r($error);
        }
        else
        {
         $data = $this->upload->data();
         $insert_data = array(
             'page_topic'=>  $this->input->post('page_name'),
             'page_desc'=>  $this->input->post('page_desc'),
             'image_url'=> $data['full_path']
         );
         $result = $this->pages_model->insert_page($insert_data);
         if(!$result){

         }
         else{
             redirect('admin/pages');  
         }
    }
   }
}