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

[eluser]Cryorus[/eluser]
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.

Controller:
Code:
function add() {
    $config = array(
        array(
                'field'   => 'title',
                'label'   => 'Título',
                'rules'   => 'trim|required|min_length[5]|max_length[50]'
            ),
        array(
                'field'   => 'img',
                'label'   => 'Imagen',
                'rules'      => 'trim'
            ),                    
        array(
                'field'   => 'intro',
                'label'   => 'Introducción',
                'rules'   => 'trim|required'
            ),
        array(
                'field'   => 'content',
                'label'   => 'Contenido',
                'rules'   => 'trim'
            )                    
        );
    
    $up_config = array(
                'upload_path'     => './resources/images',
                'allowed_types' => 'gif|jpg|png'
    );
    
    $this->load->library('upload', $up_config);
        
    $this->form_validation->set_rules($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->_thumbnailer($this->input->post('img'));                

        $this->db->insert('news', $this->db->escape($data));
        
        redirect('/admin/news/');
    }
    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);
        $this->dwootemplate->display('admin_news_add.tpl');
    }
}

View:
Code:
<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}
            <p>
                <label>Título:</label>&lt;input class="text_input" type="text" name="title" value="" size="90%" /&gt;
                <br/>
                <label>Imagen:</label>&lt;input class="text_input" type="file" name="img" size="50%" /&gt;
                <br/>                
                <label>Introducción:</label>&lt;textarea name="intro" rows="6" cols="87" size="100%" &gt;&lt;/textarea>
                <br/>
                <label>Contenido:</label>&lt;textarea name="content" rows="10" cols="87" size="100%" &gt;&lt;/textarea>
                <br/>                
                &lt;input class="submit_input" type="submit" value="Entrar" /&gt;
            </p>
        &lt;/form&gt;
    </div>

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

Thanks in advance.

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

[eluser]Cryorus[/eluser]
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.

Anyone?

Really thanks in advance, this is driving me crazy.
#3

[eluser]deczo[/eluser]
Hi there, i assume you want to insert into db uploaded file location, right?

So you can't use:
Code:
'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:
Code:
$uploadDataArray = $this->upload->data();
then:
Code:
'img'        =>    '/resources/images/' . $uploadDataArray['file_name'],

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

[eluser]Cryorus[/eluser]
Thank you, already did it this way, but i didnt know why $post didnt work.

Thanks again.




Theme © iAndrew 2016 - Forum software by © MyBB