Codeigniter - 上传png图像时出现问题

Codeigniter - Issue while uploading png image

我已经使用 Codeigniter 框架实现了用于更新个人资料图像的 Web 服务。但是我在上传 png 图像时遇到了问题。它总是给出一个错误说 -

The filetype you are attempting to upload is not allowed.

以下是我的配置数组:

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

我在允许的类型中添加了"png",仍然显示错误。 请指导。

而不是使用

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

我将配置数组更改为

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

并添加了自定义表单验证规则

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

及其对应的函数名为 file_check()。在这里,我将检查文件类型,如果它不是图像,将 return 报错。

/**
    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;
    }
}

所以我的上传功能将如下所示

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