在codeigniter中上传多个图像

时间:2017-03-10 03:20:01

标签: php image codeigniter

我已将图像上传到我的网页目录中,并将图像的名称上传到数据库中。当我上传图像时,只有图像图像被上传到目录和数据库中,一个文件被更新,但我想上传多个图像并更新多个数据库字段。

public function add_use_image(){
    include_once( dirname( __FILE__ ). '/SimpleImage.php' );
    include_once( dirname( __FILE__ ).'/_inc.php' );
    if(!empty( $_FILES['image']['name'] ) ){
    for( $i = 0; $i < count( $_FILES['image']['name'] ); $i++ ){
        $target= FCPATH. '/assets/uploads/ads/';
        $target1=$target.basename(date('m-d-Y_H:i:s').'_'.$_REQUEST['id'].'_'.@$_FILES['image']['name']);
        $img=basename(date('m-d-Y_H:i:s').'_'.$_REQUEST['id'].'_'.@$_FILES['image']['name']);
        move_uploaded_file(@$_FILES['image']['tmp_name'],$target1);
        $this->load->model( 'mads' );
        $this->mads->update( $_REQUEST['id'], array( 'image'. ( $i + 1 ) =>$img ), true );

    }
    }
        $response["success"] = 1;
        $response["image"]=$this->image_url('ads',$img);
        $response["message"] = "successfull.";

        echo json_encode($response);


}

2 个答案:

答案 0 :(得分:0)

public function add_use_image(){
    if(isset($_POST['other_doc_upload'])){
        for($i = 0; $i < count($_FILES['mul_report']['name']); $i++){
            $_FILES['cert']['name'] = $_FILES['mul_report']['name'][$i];
            $_FILES['cert']['type'] = $_FILES['mul_report']['type'][$i];
            $_FILES['cert']['tmp_name'] = $_FILES['mul_report']['tmp_name'][$i];
            $_FILES['cert']['error'] = $_FILES['mul_report']['error'][$i];
            $_FILES['cert']['size'] = $_FILES['mul_report']['size'][$i];
            $config['upload_path'] = './assets/uploads/ads/';
            $config['allowed_types'] = '*';
            $config['max_size'] = 1024 * 8;
            $config['encrypt_name'] = TRUE;
            //now we initialize the upload library
            $this->load->library('upload', $config);
            // we retrieve the number of files that were uploaded
            if (!$this->upload->do_upload('cert')){
              $msg = $this->upload->display_errors('<p>','</p>');
            }else{       
                $image = $this->upload->data();
                $fields = array(    
                    'file_ext'=> $image['file_ext'],
                    'name'=> $_FILES['cert']['name'],
                    'url'=> 'assets/uploads/ads/'.$image['file_name'],
                    'created'=> date('Y-m-d H:i:s')
                );
                $status = $this->db->insert('document', $fields);
            }
        }
        if($status == true){
            $data['message'] = 'Other Document Uploaded Successfully.';
            $data['response'] = 1;
        }else{
            $data['message'] = 'while Uploading Other Document.';
            $data['response'] = 0;
        }           
    }else{
        $data['message'] = 'while Uploading Other Document.';
        $data['response'] = 0;
    }
    echo json_encode($data);    
}

答案 1 :(得分:0)

function multiple_upload($file_key, $folder_path) {
   //////////////////// MOVE TO UPLOAD FILE TO FOLDER
   //////////////$file_key=KEY NAME OF $_FILE
   //////////////$folder_path=PATH OF DESITINAMTION FOLDER
   $j = 0;
   $totla_js = count($_FILES[$file_key]['name']);

   for ($i = 0; $i < $totla_js; $i++) {

      if ($_FILES[$file_key]["name"][$i] != "") {
         $temp = explode(".", $_FILES[$file_key]["name"][$i]);
         $extension = end($temp);
         $temp_name = date('YmdHis');
         $random1 = generateRandomString(5);
         $file_name = $temp_name.
         "_".$random1.
         ".".$extension;
         $file_path = $folder_path.
         "/".$file_name;
         if (move_uploaded_file($_FILES[$file_key]['tmp_name'][$i], $file_path)) {
            $upload[$j] = $file_name;
            $j++;
         } else {
            return "";
         }
      }
   }

   return $upload;
}


function generateRandomString($length = 2) {
   $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   $randomString = '';
   for ($i = 0; $i < $length; $i++) {
      $randomString. = $characters[rand(0, strlen($characters) - 1)];
   }
   return $randomString;
}