1

I have implemented a web service for update profile image using Codeigniter framework. But I have an issue while uploading png image. It always gives an error saying -

The filetype you are attempting to upload is not allowed.

Following is my config array:

 $config = array(
             'upload_path' => BASEPATH .'../assets/uploads/profile_images',
             'allowed_types' => 'jpg|png|jpeg',
             'overwrite' => TRUE,
             'max_size' => "2048000",
         );

I have added "png" in allowed types, still it is showing error. Please guide.

gauri
  • 121
  • 1
  • 3
  • 14
  • 1
    Possible duplicate of [Codeigniter: The filetype you are attempting to upload is not allowed](https://stackoverflow.com/questions/43024959/codeigniter-the-filetype-you-are-attempting-to-upload-is-not-allowed) – gabriella-varga Jul 05 '18 at 14:11
  • 1
    I have my CI version - 3.1.9, so I think I already have updated mimes.php file. – gauri Jul 06 '18 at 04:56

1 Answers1

2

Instead of using

   config['allowed_types'] = 'jpg|png|jpeg'

I changed my config array to

$config = array(
          'upload_path' => BASEPATH .'../assets/uploads/profile_images',
          'allowed_types' => '*',
          'overwrite' => TRUE,
          'max_size' => "2048000",
       );

And added custom form validation rule

$this->form_validation->set_rules('profile_image', '', 'callback_file_check');

and its corresponding function named file_check(). Here I will be checking file type and will return an error if it is not an image.

/**
    Function: file_check
    Description: file value and type check during validation
    Date: 6th July 2018
**/
public function file_check($str)
{
    $allowed_mime_type_arr = array('image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');

    $mime = mime_content_type($_FILES['profile_image']['tmp_name']);
    if(isset($_FILES['profile_image']['tmp_name']) && $_FILES['profile_image']['tmp_name']!="")
    {
        if(in_array($mime, $allowed_mime_type_arr))
        {
            return true;
        } else {
            $this->form_validation->set_message('file_check', 'Please select only jpg/jpeg/png file.');
            return false;
        }
    }else{
        $this->form_validation->set_message('file_check', 'Please choose a file to upload.');
        return false;
    }
}

So my upload function will look like this

if($this->form_validation->run() == true) {
    if (!$this->upload->do_upload('profile_image')) {
        return $this->response(['msg'=>$this->upload->display_errors(),'success'=>false,'code'=>400]);
    } else {
        $fileName = $this->upload->data();
        $name = $fileName['file_name'];
    }

    //Save into database
    $image = $this->Api_model->update_profile_image('assets/uploads/profile_images/'.$name, $request_data['user_id']);
    if($image)
        return $this->response(['msg'=>'Image updated successfully','success'=>true,'code'=>200, 'image_path' => base_url().$image]);
    else
        return $this->response(['msg'=>'Image updated failed','success'=>false,'code'=>400]);
    } else {
    return $this->response(['msg' => validation_errors(), 'success' => false, 'code' => 401]);
}
gauri
  • 121
  • 1
  • 3
  • 14