Skip to main content
se eliminaron 5 caracteres en el cuerpo
Origen Enlace
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload_files extends CI_Controller {
    public $images            = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
    public $pdfs              = 'pdf';

    public function addProduct(){
        $type_file            = $this->input->post('type', TRUE);
        $info_product         = array(
                                      'name' => $this->input->post('product_name', TRUE),
                                      'price'=> $this->input->post('product_price', TRUE)
                                     );

        switch($type_file){
            case 'image':
                $info_file    = $this->_uploadFile($this->images);
                break;
            case 'pdf':
                $info_file    = $this->_uploadFile($this->pdfs);
                break;
        }

        $prod_id       $msg['success']       = $this->model_insert_product->addInfoProduct($info_product);false;

        $result $prod_id              = $this->model_insert_product->addProduct>addInfoProduct($info_file, $prod_id$info_product);
 
        $msg['success']if( $info_file !== 'error'){
    = false;
       $result $msg['type']          = 'add';
$this->model_insert_product->addProduct($info_file, $prod_id);
        if( $result !== 'error'){ $msg['success']   = true;
        }

       $msg['success'] $msg['type']  = true;
       = }'add';

        echo json_encode($msg);
    }

    protected function _uploadFile($types){

        $config['upload_path']   = './image/product/');
        $config['allowed_types'] = $types;
        $config['overwrite']     = TRUE;
        $config['max_size']      = "50000";
        if( strpos($types, 'jpg')){
            $config['max_width'] = 0;
            $config['max_height']= 0;
        }

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload()){
            log_message('error', $this->upload->display_errors());
            $res                 = 'error'; 
        }
        else{ 
            $file_data           = $this->upload->data();
            $res                 = $file_data->full_path;
        }

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

class Upload_files extends CI_Controller {
    public $images            = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
    public $pdfs              = 'pdf';

    public function addProduct(){
        $type_file            = $this->input->post('type', TRUE);
        $info_product         = array(
                                      'name' => $this->input->post('product_name', TRUE),
                                      'price'=> $this->input->post('product_price', TRUE)
                                     );

        switch($type_file){
            case 'image':
                $info_file    = $this->_uploadFile($this->images);
                break;
            case 'pdf':
                $info_file    = $this->_uploadFile($this->pdfs);
                break;
        }

        $prod_id              = $this->model_insert_product->addInfoProduct($info_product);
        $result               = $this->model_insert_product->addProduct($info_file, $prod_id);
 
        $msg['success']       = false;
        $msg['type']          = 'add';

        if( $result !== 'error'){
            $msg['success']   = true;
        }

        echo json_encode($msg);
    }

    protected function _uploadFile($types){

        $config['upload_path']   = './image/product/');
        $config['allowed_types'] = $types;
        $config['overwrite']     = TRUE;
        $config['max_size']      = "50000";
        if( strpos($types, 'jpg')){
            $config['max_width'] = 0;
            $config['max_height']= 0;
        }

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload()){
            log_message('error', $this->upload->display_errors());
            $res                 = 'error'; 
        }
        else{ 
            $file_data           = $this->upload->data();
            $res                 = $file_data->full_path;
        }

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

class Upload_files extends CI_Controller {
    public $images            = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
    public $pdfs              = 'pdf';

    public function addProduct(){
        $type_file            = $this->input->post('type', TRUE);
        $info_product         = array(
                                      'name' => $this->input->post('product_name', TRUE),
                                      'price'=> $this->input->post('product_price', TRUE)
                                     );

        switch($type_file){
            case 'image':
                $info_file    = $this->_uploadFile($this->images);
                break;
            case 'pdf':
                $info_file    = $this->_uploadFile($this->pdfs);
                break;
        }

        $msg['success']       = false;

        $prod_id              = $this->model_insert_product->addInfoProduct($info_product);
        if( $info_file !== 'error'){
            $result           = $this->model_insert_product->addProduct($info_file, $prod_id);
            $msg['success']   = true;
        }

        $msg['type']          = 'add';

        echo json_encode($msg);
    }

    protected function _uploadFile($types){

        $config['upload_path']   = './image/product/');
        $config['allowed_types'] = $types;
        $config['overwrite']     = TRUE;
        $config['max_size']      = "50000";
        if( strpos($types, 'jpg')){
            $config['max_width'] = 0;
            $config['max_height']= 0;
        }

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload()){
            log_message('error', $this->upload->display_errors());
            $res                 = 'error'; 
        }
        else{ 
            $file_data           = $this->upload->data();
            $res                 = $file_data->full_path;
        }

        return $res;
    }
Origen Enlace

Asumiendo que tienes activado el .htaccess para no usar el index.php

vista

<form action="<?= base_url('generals/upload_files/addProduct')?>" method="post" enctype="multipart/form-data">
    <input type="hidden" type="image" name="type">
    <input type="text" name="product_name">
    <input type="text" name="product_price">
    <input type="file" name="userfile">
    <input type="submit" value="Upload">
</form>

application/controllers/generals/upload_files.php

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

class Upload_files extends CI_Controller {
    public $images            = 'gif|jpg|jpeg|png|GIF|JPG|PNG|JPEG';
    public $pdfs              = 'pdf';

    public function addProduct(){
        $type_file            = $this->input->post('type', TRUE);
        $info_product         = array(
                                      'name' => $this->input->post('product_name', TRUE),
                                      'price'=> $this->input->post('product_price', TRUE)
                                     );

        switch($type_file){
            case 'image':
                $info_file    = $this->_uploadFile($this->images);
                break;
            case 'pdf':
                $info_file    = $this->_uploadFile($this->pdfs);
                break;
        }

        $prod_id              = $this->model_insert_product->addInfoProduct($info_product);
        $result               = $this->model_insert_product->addProduct($info_file, $prod_id);

        $msg['success']       = false;
        $msg['type']          = 'add';

        if( $result !== 'error'){
            $msg['success']   = true;
        }

        echo json_encode($msg);
    }

    protected function _uploadFile($types){

        $config['upload_path']   = './image/product/');
        $config['allowed_types'] = $types;
        $config['overwrite']     = TRUE;
        $config['max_size']      = "50000";
        if( strpos($types, 'jpg')){
            $config['max_width'] = 0;
            $config['max_height']= 0;
        }

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload()){
            log_message('error', $this->upload->display_errors());
            $res                 = 'error'; 
        }
        else{ 
            $file_data           = $this->upload->data();
            $res                 = $file_data->full_path;
        }

        return $res;
    }

application/models/model_insert_product.php

function addInfoProduct($info_product){
    $this->db->insert('table1', $info_product);

    return $this->db->insert_id();
}

function addProduct($info_file, $prod_id){
    $this->db->insert('table2', array('fk_table1' => $product_id, 'img_path' => $info_file));

    return $this->db->insert_id();
}