1

I am stuck in uploading file in rest api in codeigniter. continously am getting the error. You do not select a file to uploadenter image description here

    $config['upload_path'] = $url.'public/images/users_pictures/';
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        $config['max_size']      = '10096000';
        $config['max_height'] = '3648';
        $config['max_width'] = '6724';
        $this->load->library('upload',$config);
        $this->upload->initialize($config);
        if($this->upload->do_upload('file'))
        {
        // $this->upload->do_upload('cover');
                $uploadData = $this->upload->data();
                $picture = $uploadData['file_name'];
        $this->db->set(array('user_image' => $picture));
        $this->db->where('user_id',$uid);
        $update = $this->db->update('asm_register');
        return $update;
        }
      else
      {
          echo $error = $this->upload->display_errors();
          return "";

      }
4
  • it means print_r($_FILES) is unpopulated. the issue is elsewhere in your code. how are you uploading? Commented Nov 5, 2018 at 20:05
  • I think the issue is in the form-submitting methods. when i switched to multipart/form-data then it's not evening picking the field value. what should i use instead? Commented Nov 6, 2018 at 6:16
  • i don't know, i can't see that code Commented Nov 6, 2018 at 6:42
  • Fine bro. let me show you in detail Commented Nov 6, 2018 at 6:44

2 Answers 2

1

Answer - Remove the Content-Type : multipart/form-data and leave that empty.

While doing on api you have to play around with file handelings.

Created a method called file_upload_check to check file, you just need to pass a file input name as parameter.

<input type="file" name="file">

Here $image_name is the name attibute of html tag.

function file_upload_check($image_name)
{
    if (isset($_FILES[$image_name]["tmp_name"]) AND  $_FILES[$image_name]['name']!="") {
        $check_image = getimagesize($_FILES[$image_name]["tmp_name"]);
        $filename   = $_FILES[$image_name]["tmp_name"];
        $handle     = fopen($filename, "r");
        $get_name   = fread($handle, filesize($filename));

        if ($check_image == false) {
            $data['error'] = '<p class="text-danger">The file you have selected is not an image.</p>';
            $data['err'] = 1;
        }else{

            // You file upload code placed here



            $data['file_name'] = $get_name;
            $data['err'] = 0;
        }
    }else{
        $data['error'] = '<p class="text-danger">Please select a file to upload.</p>';
        $data['err'] = 1;
    }

    return $data;
}



function get_file_name()
{
    // this parameter is input file name that i am passing as a parameter.
    $fileData = $this->file_upload_check('file');

    if ($fileData['err'] == 0) {

        // Here is your file name

        $file_name = $fileData['file_name'];
    }else{
        $error = $fileData['error'];
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Where did i get that $image_name variable from? that you passed in the function
I got the same issue bro ... <p class="text-danger">Please select a file to upload.</p>
I told you i didn't able to get the file to be selected ... can you explain this issue?
Now i get the point , you see the headers tab in postman you need to set the Content-Type : multipart/form-data
It work bro. while removing the Content-Type: multipart/form-data
|
0

Kindly try this one.

    if($this->upload->do_upload('file_name'))
    {
    // $this->upload->do_upload('cover');
            $uploadData = $this->upload->data();
            $picture = $uploadData['file_name'];
    $this->db->set(array('user_image' => $picture));
    $this->db->where('user_id',$uid);
    $update = $this->db->update('asm_register');
    return $update;
    }
  

Comments

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.