dptpn06684 2015-07-06 10:00
浏览 23

如何将多个上载的文件插入数据库

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条回答 默认 最新

  • dousha7645 2015-07-06 10:13
    关注

    $_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.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线