Welcome Guest, Not a member yet? Register   Sign In
Image Upload Form
#1

[eluser]Craig N[/eluser]
This is gonna be a pretty noobish post, which makes sense since I'm new to CodeIgniter Smile

I'm working on a form for my first code igniter app. I'm running into a POST issue where the post isn't passing the filename for upload. Here's the code for the form on my view:

Code:
<?=form_open_multipart('form/image_upload');?>
<?=form_hidden('form_id', $this->uri->segment(3));?>
<p>&lt;input type="file" name="image_name" size="20"/&gt;</p>
<p>&lt;input type="submit" value="Upload Image" /&gt;</p>
&lt;/form&gt;

And here's the code in the controller:

Code:
class Form extends Controller {

    function Form()
    {
        parent::Controller();
        $this->load->helper(array('url','form'));
        //$this->load->scaffolding('cb_forms');
    }

...

function image_upload(){
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('upload', $config);
        print_r($_POST);
    
        if ( ! $this->upload->do_upload($_POST['userfile']))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('form/images/'.$_POST['form_id'], $error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $this->db->insert('cb_images',$_POST);                
            $this->load->view('form/images/'.$_POST['form_id'], $data);
        }
        
        //redirect('form/images/'.$_POST['form_id']);
    }
}
Which gives me this error (I have the post array echoed out at the top of the page):

Code:
Array ( [form_id] => 1 )
A PHP Error was encountered

Severity: Notice

Message: Undefined index: userfile

Filename: controllers/form.php

Line Number: 44
An Error Was Encountered

Unable to load the requested file: form/images/1.php

Any ideas? It's not passing the userfile post variable for some reason, most likely a noobish one Smile

The form is nothing fancy, just a form for people to fill out, upload images and maybe include some fun with AJAX validation later (haven't looked into that with CI yet - are there any plugins that use jquery or other js libraries with CI? Can't imagine so since most are pretty simple already.)

edit: I'm using this page in the userguide as a reference - http://ellislab.com/codeigniter/user-gui...ading.html
#2

[eluser]xwero[/eluser]
the name of your file input needs to be userfile if you use $this->upload->do_upload(). If you want to use a self defined name you have to do $this->upload->do_upload('image_name')
#3

[eluser]Craig N[/eluser]
Ahh, I see what you did there. I was including the whole POST variable. Thanks Smile

edit: And of course it's not going to be in the $_POST array - it's in the $_FILES array lol. It's posts like these that keep me humble Smile




Theme © iAndrew 2016 - Forum software by © MyBB