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

1 Answer 1

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.

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

5 Comments

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
Can you do a var_dump($FILES); ?
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); } }
if i write this code to upload four images but only one image uploaded five times .. give me suggestion..
Use var_dump($FILES); die(); before if(isset($_FILES['image'])){ and say what do you get as output.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.