0

I am trying to upload one image through web services . following is the code

  public function upload() {
        $config['upload_path']          = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|mp4|jpeg';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        $this->load->library('upload', $config);
        $this->upload->initialize($config); 
        $this->data['data']= $_FILES;  
        echo json_encode($this->data); die; 

        if ( ! $this->upload->do_upload('userfile'))
        {
                $error = array('error' => $this->upload->display_errors());
                $this->data['data']= $error ; 
                echo json_encode($this->data['data']); 
                die; 
        }
        else
        {
                $data = array('upload_data' => $this->upload->data());
                                        $this->data['data']= 'done' ; 

                echo json_encode($this->data['data']); 
                die;
         }
}

if i json_encode($_FILES) this is the response on mobile

 data =     {
        userfile =         {
            error = 0;
            name = pen;
            size = 38238;
            "tmp_name" = "/tmp/phpEsEQNK";
            type = jpeg;
        };
    };

when i print the errors array this is what i get

error = "<p>The filetype you are attempting to upload is not allowed.</p>";

Please check both responses printed and let me know how can i fix this .

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sikander
  • 2,799
  • 12
  • 48
  • 100
  • Check the following ticket: https://stackoverflow.com/questions/9815208/codeigniter-the-filetype-you-are-attempting-to-upload-is-not-allowed – Just_Do_It Jul 25 '17 at 13:50

2 Answers2

0

Try This. This might help

$config["allowed_types"] = "image/jpeg|image/gif|image/jpg|image/png|video/mp4";
Sunil K Samanta
  • 161
  • 1
  • 8
0

Go to system/libraries/Upload.php

Then find line number 199,

$this->_file_mime_type($_FILES[$field]);

Change that line to,

$this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();

Try checking mime type here.

Now, If You are using CI version 2.1.0, then there is a bug in Upload Library,

Go To : /system/libraries/Upload.php (Line Number 1044)

Find :

$this->file_type = @mime_content_type($file['tmp_name']);
return;

Change To:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 

Find : (Line Number 1058)

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_path']), $output, $return_code);

Change To :

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code); 
D D Parmar
  • 127
  • 2
  • 11