Uploading images in CodeIgniter

1

Good,

First I wanted to say that I do not understand anything, but nothing of CodeIgniter, I never worked, I had not even heard of it. But the site is so done and I was asked to add an upload of images. And so I did, but evil. Here is the code for the " form ":

<?php echo form_open_multipart('admin/add_images_construcao');?>
<div class="form-group">
<div class="col-md-12" style="padding: 0px; margin: 0px;">
<label>Descrição</label>
<textarea name="texto" id="texto" class="col-md-12 "></textarea>
</div>
<div class="col-md-12" style="padding: 0px;">
<input type="file" name="userfile" onchange="readURL(this);" size="20" />
<input type="submit" value="Upload" class="btn btn-default"/>
</div>
</div>
</form>

This was even easy. Now comes the code that I've been working on, drawing a little bit from here and there.

 public function add_images_construcao() {

    if($this->session->userdata('logged_in'))
    {
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];

    $this->load->helper('url');
    $this->load->database();


    $id = $this->input->post('id');
    $texto = $this->input->post('texto');

    $nome_pasta = $id;

 $dir = './assets/img/construcao/'.$nome_pasta;

     if (file_exists($dir)) {
         echo '';
    } else {
        mkdir('./assets/img/construcao/' . $nome_pasta, 0777, TRUE);
    }

    $config['upload_path'] = './assets/img/construcao/'.$nome_pasta;
    $config['allowed_types'] = '*';     
    $config['max_size'] = '10000';
    $this->load->library('upload', $config);

    if(!$this->upload->do_upload()){
        $error = array('error' =>$this->upload->display_error());
        $this->load->view('admin/home', $error);

        }else{
        $file_data = $this->upload->data();
        $data['img'] = base_url().'/assets/img/construcao/'.$file_data['file_name'];
        $data = array (
        "id" => $id,
        "texto" => $texto,
        "ficheiro" => $file_data['file_name']
        );
            $this->db->insert('construcao', $data);


        $this->load->view('admin/header');
        $this->load->view('admin/navigation', $data);
        $this->load->view('admin/home', array('error'=>''));
        $this->load->view('admin/footer');          
        }
        redirect('admin/home', 'refresh');
        }   
        else
        {
        redirect('home', 'refresh');
        }
        $this->load->helper('url');
        $this->load->helper('form');

        $this->load->model('user','',TRUE);

    $data = array(
        'titulo' => $this->input->post('titulo'),
        'texto'  => $this->input->post('texto'),
        'data' => date('d-M-y')
    );

    $this->db->insert('noticias', $data);


    redirect('admin/home', 'refresh');
}

And this is what I did after 1 month .. Can anyone help me?

    
asked by anonymous 14.07.2015 / 12:51

2 answers

1

Hello

I use this method:

Controller:

   function cadastrar() {
try {     
       foreach ($_FILES as $field => $file) {
            if ($file['error'] == 0) {
                if ($this->upload->do_upload($field)) {
                    $data = $this->upload->data();
                    $dados['imagem'] = $data['file_name'];

                    #GERANDO IMAGENS THUMBS
                    $config['source_image'] = $data['full_path'];
                    $config['new_image'] = PATH_FRONT_END_UPLOAD . 'configuracoes/thumbs/';
                    $config['create_thumb'] = false;
                    $config['maintain_ratio'] = true;
                    $config['width'] = 75;
                    $config['height'] = 50;
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    #GERANDO IMAGENS MEDIUM
                    $config['source_image'] = $data['full_path'];
                    $config['new_image'] = PATH_FRONT_END_UPLOAD . 'configuracoes/medium/';
                    $config['create_thumb'] = false;
                    $config['maintain_ratio'] = true;
                    $config['width'] = 320;
                    $config['height'] = 240;

                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                } else {
                    $errors = $this->upload->display_errors();
                    die($errors);
                }
            }
        }
       $id_produto = $this->produtos_model->cadastrar_registro($dados);
    $this->session->set_userdata('is_added', true);
    redirect('cms/produtos');
} catch (Exception $e) {
    throw $e;
} }

Model:

 public function cadastrar_registro($dados) {
    try {
        $this->db->insert('tb_produto', $dados);
        return $this->db->insert_id();
    } catch (Exception $e) {
        throw $e;
    }
}

I hope it helps.

    
26.02.2016 / 11:58
0

Return an error?

Try this code:

<?php
public function add_images_construcao() {

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

    if($this->session->userdata('logged_in'))
    {
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];

    $this->load->helper('url');
    $this->load->database();


    $id = $this->input->post('id');
    $texto = $this->input->post('texto');

    $nome_pasta = $id;

 $dir = './assets/img/construcao/'.$nome_pasta;

     if (file_exists($dir)) {
         echo '';
    } else {
        mkdir('./assets/img/construcao/' . $nome_pasta, 0777, TRUE);
    }

    $config['upload_path'] = './assets/img/construcao/'.$nome_pasta;
    $config['allowed_types'] = '*';     
    $config['max_size'] = '10000';

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


    if(!$this->upload->do_upload()){
        $error = array('error' =>$this->upload->display_error());
        $this->load->view('admin/home', $error);

        }else{
        $file_data = $this->upload->data();
        $data['img'] = base_url().'/assets/img/construcao/'.$file_data['file_name'];
        $data = array (
        "id" => $id,
        "texto" => $texto,
        "ficheiro" => $file_data['file_name']
        );
            $this->db->insert('construcao', $data);


        $this->load->view('admin/header');
        $this->load->view('admin/navigation', $data);
        $this->load->view('admin/home', array('error'=>''));
        $this->load->view('admin/footer');          
        }
        redirect('admin/home', 'refresh');
        }   
        else
        {
        redirect('home', 'refresh');
        }
        $this->load->helper('url');
        $this->load->helper('form');

        $this->load->model('user','',TRUE);

    $data = array(
        'titulo' => $this->input->post('titulo'),
        'texto'  => $this->input->post('texto'),
        'data' => date('d-M-y')
    );

    $this->db->insert('noticias', $data);


    redirect('admin/home', 'refresh');
}
?>
    
16.07.2016 / 22:09