0

I'm working on a mobile application, and I'm handling database and APIs.

Android developer is sending me images and I'm using file function to get its data.

I have written this code:

public function addalbum($baseurl)
     {
        $images = array();
        if(isset($_FILES['image'])){
        //echo "hellow";exit;
            //$pathToUpload = '../media/gallary/';
            $count = count($_FILES['image']['name']);
            //echo $count;exit;
            $imagepaths = '';
            $imagetepaths = '';
            $images = '';
            for($i=0;$i<$count;$i++){
                $imageurls[$i] = $baseurl."../media/gallary/".$_FILES['image']['name'];
                $imagepaths = '../media/gallary/'.$_FILES['image']['name'][$i];

                $images[$i] = $_FILES['image']['name'][$i];
                $imagetepaths = $_FILES['image']['tmp_name'][$i];
                move_uploaded_file($imagetepaths , $imagepaths);
            }
        }       
        $data=array(
        'image' => ($images != '') ? implode(',',$imageurls) : '',
        'email'=>$this->input->post('email'),
        'name'=>$this->input->post('name'),
        'type'=>$this->input->post('type')
        );
      //print_r($data);exit;
      $this->db->insert('album',$data);
    } 

But from the bunch of images, only the last one is being inserted in the database.

any help will be very much appreciated.

Thanks

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85

1 Answers1

0

$_FILES['image'] is the field name of 1 uploaded file. If multiple files should be posted you would require different names for each field. Just think how will you upload multiple files from an html form.

For example: $_FILES['image1'], $_FILES['image2'], $_FILES['image3']

You could use something like this:

if(is_array($_FILES)) {
  foreach($_FILES as $fileKey => $fileVal){
    if($fileVal[name]) {
      move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],$target_path.$fileVal[name]);
    }
  }
}

Checkout this example where a maximum of 3 files are uploaded from android using php.

Community
  • 1
  • 1
Abey
  • 1,408
  • 11
  • 26
  • Code is showing Error to me: this code is not working in codeigniter Severity: Notice Message: Array to string conversion Filename: models/user_model.php – user3378765 Jul 06 '15 at 10:52
  • Can you do a `var_dump($FILES);` ? – Abey Jul 06 '15 at 10:57
  • my code is as per below: if(isset($_FILES['image'])){ $count = count($_FILES['image']); $imagepaths = ''; $imagetepaths = ''; $images = ''; for($i=0;$i<$count;$i++){ $imageurls[$i] = $baseurl."../media/gallary/".$_FILES['image']['name']; $imagepaths = '../media/gallary/'.$_FILES['image']['name']; $images[$i] = $_FILES['image']['name']; $imagetepaths = $_FILES['image']['tmp_name']; move_uploaded_file($imagetepaths , $imagepaths); } } – user3378765 Jul 06 '15 at 12:33
  • if i write this code to upload four images but only one image uploaded five times .. give me suggestion.. – user3378765 Jul 06 '15 at 12:38
  • Use `var_dump($FILES); die();` before `if(isset($_FILES['image'])){` and say what do you get as output. – Abey Jul 06 '15 at 12:41