简体   繁体   中英

Uploading in codeigniter doesn't work. what can be the reason?

This is the controller, and i have no errors

public function admin_page(){
    if($this->input->post('add_product')){               
        if(empty($_FILES['userfile']['name'])){
            $data = array(
                'category' => $this->input->post('category') ,
                'product_name' => $this->input->post('name') ,
                'description' => $this->input->post('description'),
                'image' => 'no_image.jpg'
            );
        } else{                     
            $config['upload_path'] = './public/images/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|JPG';
            $config['max_size'] = '1000';
            $this->load->library('upload', $config);
            $this->upload->do_upload('userfile');
            echo 'uploaded :)';
            $data = array(
                'category' => $this->input->post('category') ,
                'product_name' => $this->input->post('name') ,
                'description' => $this->input->post('description'),
                'image' => $_FILES['userfile']['name'] 
            );           
        }      
        $this->load->model('products');
        $this->products->add_product($data);
    }
    $this->load->view('admin_page');
}

View

<form action="<?php echo base_url();?>admin/admin_page" method="post" enctype="multipart/form-data">
        <div class="col-xs-3">
            <div class="form-group">
                <label for="exampleInputEmail1"> Product name </label>
                <input type="text" name="name" class="form-control" >
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1"> Category </label>
                <input type="text" name="category" class="form-control">
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1"> Description </label>
                <textarea class="form-control" name="description" rows="3"></textarea>
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1"> Image </label>
                <input type="file" name="userfile" class="form-control">
            </div>
            <input type="submit" name="add_product" class="btn btn-default" value="OK" />
        </div>
    </form>
</div>

Note: Things to check on codeigniter 3.

Make sure the controller and model name has first letter upper case Admin.php and class Admin extends CI_Controller {

And model Products.php and class Products extends CI_Model {

CI2 Upload http://www.codeigniter.com/userguide2/libraries/form_validation.html

CI3 Upload http://www.codeigniter.com/user_guide/libraries/file_uploading.html

Controller

<?php

class Admin extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('upload');

        //$this->load->model('products'); 
    }

    public function index() {

    }

    public function admin_page() {

        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('category', 'Category', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');

        if ($this->form_validation->run() == FALSE)  {

            $data['upload_errors'] = $this->upload->display_errors();

            $this->load->view('admin_page', $data);

        } else {


            /*
            * If userfile checks if.
            *
            */

            if (!empty($_FILES['userfile']['name'])) {

                /*
                *  
                *  Make sure your images upload path is on the main directory
                *
                *  application
                *  public 
                *  public > images
                *  system
                *  .htaccess
                *  index.php
                *
                */

                $config['upload_path'] = './public/images/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '3000';
                $config['max_width']  = '0';
                $config['max_height']  = '0';

                $this->upload->initialize($config);

                $field_name = 'userfile';

                if ( ! $this->upload->do_upload($field_name)) {

                    $data['upload_errors'] = $this->upload->display_errors();

                    $this->load->view('admin_page', $data);

                } else {

                    /*
                    * Using the variable below you are able to get codeigniter file upload info.
                    * Note: Codeigniter upload only made for one file
                    * $upload_data = $this->upload->data();
                    *
                    */

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

                    $data = array(
                        'category' => $this->input->post('category') ,
                        'product_name' => $this->input->post('name') ,
                        'description' => $this->input->post('description'),
                        'image' => $upload_data['file_name'] 
                    );

                    $this->products->add_product($data);

                    $data['upload_errors'] = '';

                    $this->load->view('admin_page', $data);
                }


                /*

                    If no image isset

                */

            } else {

               echo "No File Isset";


                $data = array(
                    'category' => $this->input->post('category') ,
                    'product_name' => $this->input->post('name') ,
                    'description' => $this->input->post('description'),
                    'image' => 'no_image.jpg'
                );


                $this->products->add_product($data);

                $data['upload_errors'] = '';

                $this->load->view('admin_page', $data);

            }

        }
    }
}

View

Changed input submit to button submit.

<div class="container">

<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>

<?php if ($upload_errors) {?>
    <div class="alert alert-info"><?php echo $upload_errors;?></div>
<?php }?>

<?php echo form_open_multipart('admin/admin_page');?>

    <div class="form-group">
        <label>Product name</label>
        <input type="text" name="name" class="form-control" value="<?php echo set_value('name');?>" size="50">
    </div>

    <div class="form-group">
        <label>Category</label>
        <input type="text" name="category" value="<?php echo set_value('category');?>" class="form-control" size="50">
    </div>

    <div class="form-group">
        <label>Description</label>
        <textarea class="form-control" name="description" rows="3"></textarea>
    </div>

    <div class="form-group">
        <label>Image</label>
        <input type="file" name="userfile" size="20">
    </div>

    <div class="form-group">    
        <button type="submit" class="btn btn-default">Save</button>
    </div>

<?php echo form_close();?>

</div>

I would use codeigniter's form_open_multipart() http://www.codeigniter.com/user_guide/helpers/form_helper.html

All working my end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM