Upload image with codeigniter

1

Controller:

public function generate($id,$type){
        $method = $_SERVER['REQUEST_METHOD'];
        if($method != 'POST'){
            json_output(400,array('status' => 400,'message' => 'Bad request.'));
        } else {
            $config['upload_path'] = './files/';
            $config['allowed_type'] = 'gif|jpg|png';
            $config['max_size'] = '2048';
            $config['max_width'] = '2024';
            $config['max_height'] = '2008';

            $this->load->library('upload',$config);
            if($this->upload->do_upload('fileImagen')){
                json_output($response['status'],$this->upload->display_errors());
            } else {
                $file_info = $this->upload->data();
                $descrption = $this->input->post('description');
                $location = $this->input->post('location');
                $date = date("Y-m-d");
                $time = date("H:i:s", strtotime('+12 hours'));
                //$file = $file_info['file_name'];
                $text = $this->input->post('titImagen');

               $data = array(
                  'usuario' => $id,
                  'tipo' => $type,
                  'fecha' => $date,
                  'hora' => $time,
                  'obsUsuario' => $descrption,
                  'localizacion' => $location,
                  'foto' => $text

                );

                $this->load->model('Reportmodel');
                $response = $this->Reportmodel->create($data);
            }
        }
    }

Model

public function create($data){
        $this->db->trans_start();
        $this->db->insert('reporte',$data);
        if($this->db->trans_status() == FALSE){
            $this->db->trans_rollback();
            return array('status' => 500,'message' => 'Internal server error.');
        } else {
            $this->db->trans_commit();
            return array('status' => 200,'message' => 'Registrado correctamente');
        }  
    }

It adds the data perfectly to the database, but the image does not upload it, I am sending the image via POSTMAN, since it is API, does anyone know what is wrong? the folder where the data is saved is in a folder created files in the codeigniter root.

I was looking at it possibly because of my htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Does anyone know what the error might be?

    
asked by DoubleM 22.03.2018 в 08:18
source

1 answer

1

I leave you the code of how I do it I hope it serves you

Controller:

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

        $date = $this->utilities->getDateTime();
        $id_user = $_SESSION['user_id'];
        if ($this->form_validation->run('update_user') == FALSE) {
            $this->form_user_profile($id_user);
        } else {
            $user = $this->Users_model->get($id_user);
            $user_name = $this->input->post('user_name', TRUE);
            $user_email = $this->input->post('user_email', TRUE);
            $fecha_nac = $this->input->post('user_birthdate', TRUE);
            $user_modified = $date;
            if ($_FILES['userfile']['name'] == NULL) {
                $user_img_profile = $user->user_img_profile;
            } else {

                $ram = $this->utilities->randomString(25);
                $file_name = $_FILES['userfile']['name'];
                $tmp = explode('.', $file_name);
                $extension_img = end($tmp);

                $user_img_profile = $ram . '.' . $extension_img;

                $config['upload_path'] = './assets/img/users_img/';
//              'allowed_types' => "gif|jpg|jpeg|png|iso|dmg|zip|rar|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|sxc|sxi|txt|exe|avi|mpeg|mp3|mp4|3gp",
                $config['allowed_types'] = 'gif|jpg|jpeg|png';
                $config['max_size'] = '5000000';
                $config['quality'] = '90%';
                $config['file_name'] = $user_img_profile;
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $this->session->set_flashdata('message_error', $this->upload->display_errors());
                    $this->form_user_profile($id_user);
                }
            }

            $user_db = [
                'user_email' => $user_email,
                'user_name' => $user_name,
                'user_modified' => $user_modified,
                'user_img_profile' => $user_img_profile,
                'user_birthdate' => $fecha_nac,
            ];
            $id = $this->Users_model->update($id_user, $user_db);
            if ($id > 0) {
                $_SESSION['user_img'] = $user_img_profile;

                $ls = $this->utilities->add_log("Actualizacion de Perfil ID: " . $id_user, $_SESSION['user_id']);
                $this->utilities->set_message('perfil', 'message_success', $this->lang->line('users_success_update_profile'));
            } else {
                $this->utilities->set_message('perfil', 'message_error', $this->lang->line('users_error_update_profile'));
            }
        }
    }

Model:

class Users_model extends CI_Model {

    //put your code here
    private $table = "users";
    private $id = "id";

public function __construct() {
        parent::__construct();
    }

public function update($id, $data) {
        $this->db->where($this->id, $id);
        $this->db->update($this->table, $data);
        return $this->db->affected_rows();
    }
}
    
answered by 22.03.2018 / 08:34
source