File name not grabbed when using CodeIgniter upload library

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.

Answers


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');
        }
    }

Need Your Help

Assets folder in Android Studio?

android android-studio typeface custom-font

Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. I'm using Android Studio and it doesn't seem like I have a assets folder. So I created one. But my app

UIScrollView jumps down when first touched

iphone ios cocoa-touch uiscrollview

I have two UIScrollViews, one horizontal, one vertical, both two pages each (as in, the horizontal one is two pages wide, and the vertical one is two pages high). The horizontal scrollview is in the