Welcome Guest, Not a member yet? Register   Sign In
Empty file upload field
#1

[eluser]tj530[/eluser]
Currently, i can hit upload in my form without choosing a file and it will still run the code. I tried adding code so that my code would stop if there is no file chosen to upload but it wont work. Can someone please help me with this.


MY FUNCTION
Code:
function upload_image($field = 'userfile')
    {
        // this was the code that was supposed to check to see if there was any file chosen but it doesnt work at all. the code still runs even when no file is chosen...
        if ( ! isset($_FILES[$field])){
        return FALSE;
        } else {
            
            if ($this->input->post('upload')) {
                $userid = $this->session->userdata('userid');
                
                $user_row = $this->users_model->get_user_info($userid);
                    $old_image = $user_row->image_path;
                    
                    if ($old_image != 'default_image_small.png') {
                        
                        $thumbfilename = SITEPATH.'images/thumbs/'.$old_image;
                        if (file_exists($thumbfilename)) {
                            unlink($thumbfilename);
                        }    
                        $filename = SITEPATH.'images/'.$old_image;
                        if (file_exists($filename)) {
                            unlink($filename);
                        }
                    }
                    
                $this->images_model->do_upload($userid);
                $user_row = $this->users_model->get_user_info($userid);
                $success = "Your image was successfully uploaded";
                $data = array(
                    'menu tab' => 'home',
                    'user_row' => $user_row,
                    'success' => $success
                    );
                $this->load->view('users/user_success', $data);
            }
        }
    }

My View
Code:
<?php
echo form_open_multipart('user/upload_image');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>

My Model
Code:
function do_upload($userid) {
        
        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->image_path,
            'max_size' => 2000
        );
        
        
        
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $image_data = $this->upload->data();
        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->image_path . '/thumbs',
            'maintain_ration' => true,
            'width' => 75,
            'height' => 75
        );
        
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
        
        $update_image = $image_data['file_name'];
        $new_image_data = array(
            'image_path' => $update_image
        );
        
        $this->db->where('id', $userid);
        $this->db->update('users', $new_image_data);
    }
}
#2

[eluser]Madmartigan1[/eluser]
if ($this->input->post('upload'))...

Im not 100% sure without seeing your whole code, but you should be checking the $_FILES array instead of the $_POST array.

Is 'upload' the name of the file upload field?

EDIT: Sorry I missed that line. Ignore me :down:

Take a look at the upload class.
#3

[eluser]tj530[/eluser]
i just want to know why this doesnt work?!:
Code:
if ( ! isset($_FILES[$field])){
        return FALSE;
        } else { ...
#4

[eluser]Madmartigan1[/eluser]
Try checking if $_FILES['userfile'] is empty instead of set.
#5

[eluser]tj530[/eluser]
i did this just now and it didnt work
Code:
if (empty($_FILES[$field])){
        return FALSE;
        }
#6

[eluser]tj530[/eluser]
ok nvm im stupid. i got it. small stupid mistakes are my worst enemies!




Theme © iAndrew 2016 - Forum software by © MyBB