Upload images to Codeigniter

Asked

Viewed 2,057 times

1

Good,

At first I wanted to say that I don’t understand anything, but I really don’t know anything about Codeigniter. But the site is so done and asked me to add an image upload. And so I did, but badly. Here’s 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 quite easy. Now comes the code I’ve been working on, taking a little bit of that 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 a month.. Someone can help me?

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.

0

Returns 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');
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.