1

How do I upload an image when I'm trying to save other data along with it? When the form submits, it hits the save function:

function save() {
   $this->save_data($_POST, $_FILES);
}    

function save_data($post_data, $file_data) {
    // if theres an image
    if(!empty($file_data['image']['size'])) {
        $path = '/images';
        $this->upload_image($path);
    }
}

function upload_image($path) {
    // CI is looking for $_FILES super global but I want to pass that data in
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $this->load->library('upload', $config);
    $this->upload->data('image');
    $this->upload->do_upload('image');
}

I can't figure out how to actually pass the file data to another function. All the examples I've seen shows the form submitting to a function that uploads the function right from it. I want to do the uploading from another function though.

1
  • Never had any problem performing the upload from another function (via Codeigniter or otherwise). Does the above code work when you use it directly from the submit function ? Commented Mar 23, 2014 at 18:33

2 Answers 2

1

if you are trying to check if file is actually beeing uploaded do following

//this is optional
if (empty($_FILES['userfile']['name'])) {

    $this->form_validation->set_rules('userfile', 'picture', 'required');

}

if ($this->form_validation->run()) { //if using validation
    //validated
    if (!empty($_FILES['userfile']['name'])) {
        //picture is beeing uploaded

        $config['upload_path'] = './files/pcitures';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['encrypt_name'] = TRUE;

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

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

            //$error = array('error' => $this->upload->display_errors());

        } else {

            //no error, insert/update in DB
            $tmp = $this->upload->data();
            echo "<pre>";
            var_dump($tmp);
            echo "</pre>";
        }

    } else { ... }

}
Sign up to request clarification or add additional context in comments.

Comments

1

My mistake was related to folder permissions.

For those of you that are looking to split the upload functionality into multiple functions:

Controller:

$save_data = $this->save_model->save_data($_POST, $_FILES);

Model:

function save_data($data, $file) {
    // check if theres an image
    if (!empty($file['image']['size'])) {
        // where are you storing it
        $path = './images';
        // what are you naming it
        $new_name = 'name_' . random_string('alnum', 16);
        // start upload
        $result = $this->upload_image($path, $new_name);
    }
}

function upload_image($path, $new_name) {
    // define parameters
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';
    $config['file_name'] = $new_name;
    $this->load->library('upload', $config);
    // upload the image
    if ($this->upload->do_upload('image')) {
        // success
        // pass back $this->upload->data() for info
    } else {
        // failed
        // pass back $this->upload->display_errors() for info
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.