【问题标题】:Upload multiple images in codeigniter在codeigniter中上传多张图片
【发布时间】:2017-07-31 06:41:16
【问题描述】:

我已经上传了我的 web 目录中的图像和数据库中的图像名称。当我上传图像时,目录中只有图像图像上传,数据库一个文件没有更新,但我想上传多个图像并更新多个数据库字段。

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);


}

【问题讨论】:

    标签: php image codeigniter


    【解决方案1】:
    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);    
    }
    

    【讨论】:

      【解决方案2】:
      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-04
        • 1970-01-01
        • 2017-04-08
        • 2011-12-10
        • 2016-01-06
        • 2017-05-28
        相关资源
        最近更新 更多