Welcome Guest, Not a member yet? Register   Sign In
Weird problem with POST and the upload class

Hi everyone. Well, im a newbie learning this thing, pretty cool indeed. The site: http://cryorus.ignavus.org/gedn/

In my admin panel, im trying to add news to the database with an image, submitting the info to my database. Submitting it was perfect till i added the file upload class, it uploads perfectly and i can still insert the info to the database, everything but the 'img' field. I did some debug and $this->input->post('img'), is empty. Now im insering to the database: "/resources/images/"

Im using dwoo for templates, so im not using any php code in my view files.

Here is my code.

function add() {
    $config = array(
                'field'   => 'title',
                'label'   => 'Título',
                'rules'   => 'trim|required|min_length[5]|max_length[50]'
                'field'   => 'img',
                'label'   => 'Imagen',
                'rules'      => 'trim'
                'field'   => 'intro',
                'label'   => 'Introducción',
                'rules'   => 'trim|required'
                'field'   => 'content',
                'label'   => 'Contenido',
                'rules'   => 'trim'
    $up_config = array(
                'upload_path'     => './resources/images',
                'allowed_types' => 'gif|jpg|png'
    $this->load->library('upload', $up_config);
    if ($this->form_validation->run() && $this->upload->do_upload('img')) {
        $data = array(
                    'title'        =>    $this->input->post('title'),
                    'img'        =>    '/resources/images/' . $this->input->post('img'),
                    'intro'        =>    $this->input->post('intro'),
                    'content'    =>    $this->input->post('content')

        $ext = strstr($this->input->post('img'), '.');
        $data['img_thumb'] = substr($data['img'], 0, -strlen($ext)) . '_thumb' . $ext; //done!


        $this->db->insert('news', $this->db->escape($data));
    else {
        $up_errors = $this->upload->display_errors('<h2 class="header error">', '</h2>');
        $errors = validation_errors();
        $this->dwootemplate->assign('errors', $errors);
        $this->dwootemplate->assign('up_errors', $up_errors);

<div id="news">    
        <h2 class="header center">Noticias</h2>
        &lt;form class="default addnew" action="/gedn/admin_news/add/" method="post" enctype="multipart/form-data"&gt;
            {if $errors!=""}{$errors}{/if}
            {if $up_errors!=""}{$up_errors}{/if}
            {if $db_errors}<h2 class="header error">{$db_errors}</h2>{/if}
                <label>Título:</label>&lt;input class="text_input" type="text" name="title" value="" size="90%" /&gt;
                <label>Imagen:</label>&lt;input class="text_input" type="file" name="img" size="50%" /&gt;
                <label>Introducción:</label>&lt;textarea name="intro" rows="6" cols="87" size="100%" &gt;&lt;/textarea>
                <label>Contenido:</label>&lt;textarea name="content" rows="10" cols="87" size="100%" &gt;&lt;/textarea>
                &lt;input class="submit_input" type="submit" value="Entrar" /&gt;

Im pretty sure we dont need to know any of the other code.

Thanks in advance.

Edit: if in wrong section, please move, sorry.

This isnt an easy thing as it looks?

Edit: Ok, i added the rule for the img field "required" and when i try to submit, i get an error saying that an image is required, so... something is removing the content inside my 'img' field.


Really thanks in advance, this is driving me crazy.

Hi there, i assume you want to insert into db uploaded file location, right?

So you can't use:
'img'        =>    '/resources/images/' . $this->input->post('img'),
because uploaded file data resides in $_FILES not in $_POST.

Anyway in CI just use upload data helper, part of the upload library:
after upload add:
$uploadDataArray = $this->upload->data();
'img'        =>    '/resources/images/' . $uploadDataArray['file_name'],

You can use $uploadDataArray['full_path'] instead, for details check http://ellislab.com/codeigniter/user-gui...ading.html

Thank you, already did it this way, but i didnt know why $post didnt work.

Thanks again.

Theme © iAndrew 2016 - Forum software by © MyBB