2
votes

As described in the question above, I'm trying to upload a file from okhttp3 to the server.

I'm always receiving 'You did not select a file to upload' in my approach from codeIgniter part.

Here's my code

Android:


    public void sendItem(File item) {
        Response response = null;
        MediaType mediaType = MediaType.parse(FileUtils.getMimeType(item));
        try {
            RequestBody formBody = new MultipartBody.Builder().addFormDataPart("item", item.getName(), RequestBody.create(mediaType, item)).build();

            Request request = new Request.Builder().url("index.php").post(formBody).build();

            response = client.newCall(request).execute();
            int statusCode = response.code(); // always 500

        } catch (IOException e) {
            e.printStackTrace();

        } finally {
            if (response != null)
                response.close();
        }
    }

Server Code:


    function upload(){
        $config['upload_path'] = '.../assets/...';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        $media = 'item';
        if (!$this->upload->do_upload($media)) {
            $error = array('error' => $this->upload->display_errors());
            print_r($error);
        }else{
            $stuff = $this->upload->data();
            return $stuff ;
        }
    }

What am i doing wrong? Thanks!

2
What do you have in $media ?Kristiyan
codeigniter upload library works only on php version 5.6 and above so make sure that is correct.Jude Fernandes

2 Answers

0
votes

I am pretty sure that CodeIgniter requires you to set the $config['allowed_types'] as part of the library initialization. The default installation doesn't set 'allowed types' so you must explicitly set it in your controller or in a config file per the documentation.

You need to add something like $config['allowed_types'] = 'gif|jpg|png'; before you initialize your library.

That said, this might not be your only issue.

0
votes

Can you do a print_r($_FILES); in your server code & see if the file data is being sent in that variable?

The message of 'You did not select a file to upload' is shown from CodeIgniter's Upload library only when no file has been sent in $_FILES.

And you should also try echoing print_r($_POST); to ascertain if your file data is coming in $_POST or in $_FILES.