Search code examples
phpcodeignitercodeigniter-3

JSON Upload Codeigniter 3 The filetype you are attempting to upload is not allowed


I'm trying to build a file uploader to upload JSON files using Codeigniter 3.1.13. I built the basic form using the CI documentation but I keep getting the error "The filetype you are attempting to upload is not allowed."

I did a var_dump of the file, and checked that the type is indeed listed in mimes.php, so I'm not sure what I'm missing!

Here is my code:

    public function upload_data_file(){
        $this->load->helper(array('form', 'url'));


        $config['upload_path']          =$this->config->item('temp_path') . "fhmFieldData";
        $config['allowed_types']        = 'json';

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

        var_dump($_FILES);

        if ( ! $this->upload->do_upload('userfile')) {
                $error = array('error' => $this->upload->display_errors());
                $this->render($error,'fhm/enter_data');
        }
        else {
                $data = array('upload_data' => $this->upload->data());
                $this->render($data,'fhm/enter_data');
        }
    }

This is what the var_dump prints:

  'userfile' => 
    array (size=5)
      'name' => string 'FHMFieldData_v2022-1_VMC593.json' (length=32)
      'type' => string 'application/json' (length=16)
      'tmp_name' => string '/users/v/m/vmc/phptemp/upload/php846QNI' (length=39)
      'error' => int 0
      'size' => int 17572

My mimes.php file has this on line 117:

    'json'  =>  array('application/json', 'text/json'),

Solution

  • making my comments an answer:

    1. Check your server's mime types, because not all hosting providers install all the mime types!

    2. add text/plain to your 'json' in application\config\mimes.php:

      'json' => array('application/json', 'text/json', 'text/plain'),

    note: adding text/plain also often resolves *.csv mime type issues, so keep that in mind for other applications