0

I am working on codeigniter and during creation of one of the APIs I got the issue. I tried to upload the image on Server as a file, while searching on the web, I got familiar with inbuild upload class in codeigniter. Please have a look at this code. I am sending file from Android using this tutorial.

public function upload_image_post(){

 $config['upload_path'] =base_url().'/uploads/';
    $config['file_name'] = rand() .'.jpg';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = 10000;
    $config['remove_spaces'] = TRUE;
    $config['encrypt_name'] = TRUE;
    // $file  = $this->input->post('file');

    $this->load->library('upload', $config);
// $this->upload->initialize($config);
    $file=$_FILES['uploaded_file'];
    // $this->upload->do_upload($file);
    if($file){
      $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'A',
        'sp_id'=>'asQ',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');
        /* This is working*/
        $res = $this->db->insert('ww_portfolio_images',$content);     
      }else{
         $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'not file',
        'sp_id'=>'asQaaaaa',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');
        /* This is not working, Thats Obvious*/
        $res = $this->db->insert('ww_portfolio_images',$content);     

      }

      // $destinationPath=APPPATH.'public/assets/uploads/ANKO.jpg';
    if($this->upload->do_upload('uploaded_file')) {
      $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'A',
        'sp_id'=>'aaaaaaaaaaaaaaaaas',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');
        /* This is not working*/
        $res = $this->db->insert('ww_portfolio_images',$content);
     $this->response(['result' =>'Success',]  , REST_Controller::HTTP_OK);              
         // return ($arr_image_info['full_path']);
    }
    else{
     $content=array(
        'image_id'=>'IMG'.rand(),
        'album_id'=> 'A',
        'sp_id'=>'asass',
        'image_name'=>'aAA',
        'status'=>1,
        'tags'=>'s');

        $res = $this->db->insert('ww_portfolio_images',$content);
      /* This is working*/
      $this->response(['result' => 'ERrro']  , REST_Controller::HTTP_OK);    
        // $this->response(['result' =>'Image error',], 433);                
    }
   }

I can not figure out the problem I am facing here. I am receiving a file but it does not upload.

I have also tried to use $this->upload->do_upload() instead of $this->upload->do_upload('uploaded_file') and this $config['max_size'] = '10000'; instead of this $config['max_size'] = 10000; . Please help. Any help would be greatly appreciated.

Also, when this code run through web panel, it working fine.

NID
  • 3,238
  • 1
  • 17
  • 28

3 Answers3

0

Better if you provide some more detail for the type of error or warning that you are observing. There could be number of reasons.

1) base_url() gives you the public URL. You have to specify the absolute or relative path to your upload folder.

2) (if you are using an Apache Server) Your Apache user don't have the write permission to the upload folder.

3) Folder path doesn't exists.

If the first one doesn't work, please check the rest of the points. Hope this help you.

Regards Muaaz

Muaaz Khalid
  • 2,199
  • 2
  • 28
  • 51
0

Try using FCPATH

$config['upload_path'] = FCPATH . '/uploads/';

Or

$config['upload_path'] = './uploads/';

Then http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-controller

<?php

class Example extends CI_Controller {

public function __construct() {
  parent::__construct();
  $this->load->helper('form');
}

// Name function what every you want remember to change it on view form.
public function upload() {

$config['upload_path']          = './uploads/';
$config['allowed_types']        = 'gif|jpg|png';
$config['max_size']             = 100;
$config['max_width']            = 1024;
$config['max_height']           = 768;

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

if ($this->upload->do_upload('uploaded_file')) {

   // Then the success stuff

   $upload_data = $this->upload->data();

   echo $upload_data['file_name'];

} else {
   // Errors
}

}

}

On view I would use the form helper functions form_open_multipart()

<?php echo form_open_multipart('example/upload');?>
<?php echo form_upload('uploaded_file', 'Upload');?>
<?php echo form_close();?>
0
  • check your form input in the view

My form view :

<input id="document" type="file" data-browse-label="browse" name="document" data-show-upload="false" data-show-preview="false" class="form-control file" />
  • other can be permission issue for the uploads folder if the folder does not exist you have to create it

And always try to debug the code with logs

document in the do_upload is the name of the input element in my view

       if ($_FILES['document']['size'] > 0) {
            $this->load->library('upload');
            $config['upload_path'] = 'uploads/images';
            $config['allowed_types'] = '*';
            $config['max_size'] = $this->allowed_file_size;
            $config['overwrite'] = false;
            $config['encrypt_name'] = true;
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('document')) {
                $error = $this->upload->display_errors();
                $this->session->set_flashdata('error', $error);
                redirect($_SERVER["HTTP_REFERER"]);
            }
            $photo = $this->upload->file_name;
        }
Arun pandian M
  • 862
  • 10
  • 17