1

I've been stuck trying to upload a file in a module I am buidling.

I've tried using the EE instance and now I am trying the CI instance.

I've checked permission settings and those are fine for the directory I am trying to upload to.

I set the form type to use the form_open_multipart

Here is my upload function that does not want to work:

function upload_file()
{
    $this->CI =& get_instance();

    $file = $this->EE->input->post('userfile');

    $config['upload_path'] = './images/';
    $config['allowed_types'] = 'gif|jpg|png';
    $this->CI->load->library('upload', $config);

    if ( ! $this->CI->upload->do_upload($file) )
        print 'could not upload file';
}
2

A few things that should help you out.

1) You should be using the EE instance. Normally you to the get_instance() in the constructor of your module and make it available as a class variable.

2) The do_upload method accepts a string with the name of the file upload field, not the value of the posted field.

Your code should look somehting like this

// Configure the upload class
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->EE->load->library('upload', $config);

// Error checking
if ( ! $this->EE->upload->do_upload('file_field_name'))
{
    $data = array(
      'error' => $this->EE->upload->display_errors()
    );

}
// No problems so do our processing
else
{
    // Store the info about the uploaded file
    $upload_data = $this->EE->upload->data();
}

This was taken directly from a module I wrote that uploads files, so should work for you with some minor adjustments.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.