2

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 ? Commented Jun 21, 2016 at 13:59
  • codeigniter upload library works only on php version 5.6 and above so make sure that is correct. Commented Jul 6, 2017 at 17:06

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

allowed_types is not mandatory, it can be placed if you'd like to limit to certain extensions.
Disagree. You can create a global config file for file uploads and set $config['allowed_types'] = "*"; Or you can set it in the controller before initialization. If you don't set it at all, the library will refuse to work. The documentation for this library is pretty bad.
I totally missed the global config part, you're correct here @Wade
0

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.

2 Comments

that's not an answer at all.
Sure, that might not be the answer. But the do_upload() of CodeIgniter upload library looks in the $_FILES array for the 'item' variable, so it will look for $_FILES['item'] element. Unless you are getting that, this message is going to come.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.