Lewis
Lewis

Reputation: 225

codeigniter file upload with other elements

I've got a page with a form. On the form is a dropdownlist and a few inputboxes and a file upload. If I try to submit my page I get a blank page with the error 'Non-existent class: Upload'. I hadn't use file upload yet.

This is my input file type:

<tr><td>Image: </td></tr>
<tr><td><input type="file" name="image" size="20" /></td></tr>

I've read on the CI file uploading class that you need a form_open_multipart('upload'); So will I just have that form or will I have to do something in else in my case? Because I got other input types on that form?

Here is a little bit code of my controller:

$subject = htmlspecialchars(trim($this->input->post('subject')));
$message = htmlspecialchars(trim($this->input->post('message')));
$image = $this->input->post('image');

$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);

if (!$this->upload->do_upload($image)) {          
    $data['fout'] = $this->upload->display_errors();
    $this->annuleren($soort, $data);
} else {
    $data['upload'] =  $this->upload->data();
}

$id = $this->message_model->insert($message, $subject, $image);

redirect('infoscreen/dashboard', 'refresh');

I've also made a folder 'uploads' under 'source files'. I'm probably doing something stupid. Can someone please help me out and tell me what I'm doing wrong and how I could fix it?

Thanks alot :)

Upvotes: 0

Views: 2924

Answers (1)

jtheman
jtheman

Reputation: 7491

EDIT: First load the upload library:

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

Another problem lies in the fact that you're referencing the file wrong.

This row is unneccesary:

$image = $this->input->post('image'); // <- remove this line

Instead just use:

if (!$this->upload->do_upload('image')) {   

CodeIgniter by default is looking for a file input named 'userfile' but if that's not present you need to add 'image' to the do_upload() method like this.

Then you have the line:

$id = $this->message_model->insert($message, $subject, $image);

I'm not certain what you think should be in the $image variable but if the upload succeded you can attach data from $data['upload']:

$id = $this->message_model->insert($message, $subject, $data['upload'][file_name]); // <- gets the filename 

Full reference here: http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Also to answer your second question it is not a problem that the multipart form type has other input fields/types.

Upvotes: 2

Related Questions