0

Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.

I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.

My form:

    <?php echo form_open_multipart('image_upload/do_upload');?>

    <input type="file" name="userfile" size="20" multiple="true" />

    <br /><br />

    <input type="submit" value="upload" />

    </form>

My Controller function:

    function do_upload()
     {
        $config['upload_path'] = 'c:/wamp/www/honest/images';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;


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

        $image_data = $this->upload->data();

        echo '<pre>'; print_r($image_data); echo '</pre>';

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $data['main_content'] = 'imageupload';
            $this->load->view('includes/template', $data);
            echo "FAILED";
        }
         else
        {
          $data = array(
            'filename' => $image_data['file_name'],
            'fullpath' => $image_data['full_path']
          );
          $this->db->insert('images', $data);   
          $this->load->view('imageupload');
        }
    }

I am using

    echo print_r($image_data); echo;

To display the associated image data but this is all that is returned:

Array
(
    [file_name] => 
    [file_type] => 
    [file_path] => c:/wamp/www/honest/images/
    [full_path] => c:/wamp/www/honest/images/
    [raw_name] => 
    [orig_name] => 
    [client_name] => 
    [file_ext] => 
    [file_size] => 
    [is_image] => 
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
)

I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?

Many thanks,

DP.

2
  • you question seem like similar with this post : stackoverflow.com/questions/8643806/… Commented Apr 7, 2016 at 15:17
  • Yup - read that one, didn't work. But another poster has just spotted the error. Commented Apr 7, 2016 at 15:52

1 Answer 1

3

You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):

     function do_upload()
     {
        $config['upload_path'] = 'c:/wamp/www/honest/images';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;


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

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $data['main_content'] = 'imageupload';
            $this->load->view('includes/template', $data);
            echo "FAILED";
        }
         else
        {
          $image_data = $this->upload->data();
          $data = array(
            'filename' => $image_data['file_name'],
            'fullpath' => $image_data['full_path']
          );
          $this->db->insert('images', $data);   
          $this->load->view('imageupload');
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! - thanks loads Wolf. This has baffled me all day. I imagined that the $this->load->library('upload', $config); had already performed the upload - apparently not!
@DavidPride You're welcome. Feel free to mark my answer as correct.

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.