我想上传两张图片,但只插入一张图片

时间:2019-07-11 11:09:56

标签: codeigniter-3

我想分别上传2张图片。我有一个条件,我需要上传文件,有时需要上传一些图像,有时需要仅上传图像,我需要使用单独的按钮来区分图像和文件。

我的代码 查看

<?php echo form_open_multipart('upload/file_data');?>

  <div class="form-group">
    <label for="product_name">Product Name*:</label>
    <input type="text" class="form-control" name="product_name" value="<?= set_value('product_name'); ?>" id="product_name">
  </div>
  <div class="form-group">
    <label for="product_price">product price :</label>
    <input type="text" class="form-control" name="product_price" value="<?= set_value('product_price'); ?>" id="product_name">
  </div>
  <div class="form-group">
    <label for="product_image">Select Image*:</label>
    <input type="file" name="product_image" class="form-control"  id="product_image">
  </div>
  <div class="form-group">
    <label for="product_banner">Select banner Image*:</label>
    <input type="file" name="product_banner" class="form-control"  id="product_banner">
  </div>
   <div class="form-group">
    <label for="cat_id">cat :</label>
     <select name="cat_id" id="cat_id">
  <option value="Veg">Veg</option>
  <option value="Fruits">Fruits</option>
  <option value="Snacks">Snacks</option>

</select> 
  </div>
  <a href="<?=base_url();?>" class="btn btn-warning">Back</a>
  <button type="submit" class="btn btn-success">Submit</button>
</form>

这是控制器

public function file_data(){
        //validate the form data 

        $this->form_validation->set_rules('product_name', 'product_price', 'required');

        if ($this->form_validation->run() == FALSE){
            $this->load->view('upload_form');
        }else{

            //get the form values
            $data['product_name'] = $this->input->post('product_name', TRUE);
            $data['product_price'] = $this->input->post('product_price', TRUE);
            $data['cat_id'] = $this->input->post('cat_id', TRUE);

            //file upload code 
            //set file upload settings 
            $config['upload_path']          = APPPATH. '../assets/uploads/';
            $config['allowed_types']        = 'gif|jpg|png';


            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload('product_banner')){
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);
            }
            else{

                //file is uploaded successfully
                //now get the file uploaded data 
                $upload_data = $this->upload->data();

                //get the uploaded file name
                $data['product_image'] = $upload_data['file_name'];
                $data['product_banner'] = $upload_data['file_name'];


                //store pic data to the db
                $this->pic_model->store_pic_data($data);

                redirect('/');
            }
            $this->load->view('footer');
        }
    }

我想分别上传2张图片。我有一个条件,我需要上传文件,有时需要上传图片,有时需要仅上传图片,我需要使用单独的按钮来区分图片和文件。

1 个答案:

答案 0 :(得分:0)

尝试一下:

case 1: 
matrix = [[1,2,3],[4,5,6],[7,8,9]]
matrix = matrix[::-1]
for i in range(len(matrix)):
    for j in range (len(matrix[0])):
        if i<=j:
            matrix[j][i],matrix[i][j] =matrix[i][j], matrix[j][i]

print(matrix)

case 2: 
def rotate(matrix) -> None:
    print('1',matrix)
    matrix = matrix[::-1]
    print('2',matrix)
    for i in range(len(matrix)):
        for j in range (len(matrix[0])):
            if i<=j:
                matrix[j][i],matrix[i][j] =matrix[i][j], matrix[j][i]
    print('3',matrix)

matrix = [[1,2,3],[4,5,6],[7,8,9]]
rotate(matrix)
print(matrix)


case 1 output:
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]

case 2 output:
1 [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
2 [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
3 [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
[[9, 6, 3], [8, 5, 2], [7, 4, 1]]

使新功能上传图片和横幅

public function file_data(){
    //validate the form data 

    $this->form_validation->set_rules('product_name', 'product_price', 'required');

    if ($this->form_validation->run() == FALSE){
        $this->load->view('upload_form');
    }else{

    //get the form values
    $data['product_name'] = $this->input->post('product_name', TRUE);
    $data['product_price'] = $this->input->post('product_price', TRUE);
    $data['cat_id'] = $this->input->post('cat_id', TRUE);
    $image = $this->product_image(); // this function upload image and return image name
    $banner = $this->product_banner(); // this function upload baner and return banner name

            //get the uploaded file name
            $data['product_image'] = $image;
            $data['product_banner'] = $banner;

            //store pic data to the db
            $this->pic_model->store_pic_data($data);
            redirect('/');
    }
}