Q:

image validation using image header in codeigniter to prevent hacking

I want a method for validating image files.
I have tried validating using image type and its formats and size. I need another validation with high accuracy.
I am asking because my site has hacked by an uploaded image file containing code. The hacker has written code in .png format and uploaded it as an image in my site. From that moment he started hacking my site.
Kindly help me to get a high validation for an image file using its header.
I have validation like below:
 function upload()
    {
        //set preferences
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'txt|pdf';
        $config['max_size']    = '100';

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

        if (!$this->upload->do_upload('filename'))
        {
            // case - failure
            $upload_error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_file_view', $upload_error);
        }
        else
        {
            // case - success
            $upload_data = $this->upload->data();
            $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
            $this->load->view('upload_file_view', $data);
        }
    }

A:

You have allowed filetypes of txt and pdf. You should use 
$config['allowed_types'] = 'gif|jpg|png';

If it is images you are uploading.

