使用codeigniter上传具有不同名称的多个图像

时间:2016-09-06 11:56:10

标签: php codeigniter

我正在尝试从html表单上传三个图像,有三个不同的名称。但是它显示错误

$config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['file_name']            =  $this->input->post('mobile_name').'_front.jpg';
                $config['overwrite']            =  TRUE;

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


                if ($this->upload->do_upload('files'))
                {
                        $_POST['mobile_img1']= $this->upload->data()['file_name'];
                }
                else
                {
                      echo "image upload fails";
                      exit();
                }

这是我的codeigniter代码,用于上传一张图片。

我在html代码中有三个图像输入字段

 <div class="row">
                                  <div class="col-md-4 col-md-offset-4">

                                    <div class="form-group">
                                                        <label class="control-label">Mobile Image Front</label>
                                                        <input type="file" name="mobile_img1" class="filestyle" data-placeholder="No file">
                                    </div>
                                    <div class="form-group">
                                                        <label class="control-label">Mobile Image Back</label>
                                                        <input type="file" name="mobile_img2" class="filestyle" data-placeholder="No file">
                                    </div>
                                    <div class="form-group">
                                                        <label class="control-label">Mobile Image Side</label>
                                                        <input type="file" name="mobile_img3" class="filestyle" data-placeholder="No file">
                                    </div>

                                  </div>
                              </div> 

在这个html字段中我需要将第一个输入上传为mobilename_front.jpg,第二个作为mobilename_back.jpg上传,第三个作为mobilename_side.jpg上传,我还需要在db中上传这些名称。给出一些实现这个的想法,

3 个答案:

答案 0 :(得分:1)

查看文件

<div class="row">
<div class="col-md-4 col-md-offset-4">
<form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" enctype='multipart/form-data'>
<div class="form-group">
                    <label class="control-label">Mobile Image Front</label>
                    <input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
                    <label class="control-label">Mobile Image Back</label>
                    <input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
                    <label class="control-label">Mobile Image Side</label>
                    <input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
  <input type="submit" value="upload"></form>
</div>
</div>

我的控制器:

<?php
class My_Controller extends CI_Controller {
public function __construct() {
  parent::__construct();
  $this->load->library('upload');
}
public function file_upload(){
  $files = $_FILES;
  $cpt = count($_FILES['mobile_img']['name']);
  for($i=0; $i<$cpt; $i++)
  {
      $mobileView=(($i == 0)?"_front":(($i == 1)?"_back":"_side")); //Here i assign first image(i = 0) as _front, second image(i = 1) as _back,third image(i = 2) as _side
      $_FILES['mobile_img']['name']= $files['mobile_img']['name'][$i].$mobileView.time();// time() for time in seconds becauze we may need if same name uploaded file name
      $_FILES['mobile_img']['type']= $files['mobile_img']['type'][$i];
      $_FILES['mobile_img']['tmp_name']= $files['mobile_img']['tmp_name'][$i];
      $_FILES['mobile_img']['error']= $files['mobile_img']['error'][$i];
      $_FILES['mobile_img']['size']= $files['mobile_img']['size'][$i];
      $this->upload->initialize($this->set_upload_options());
      $this->upload->do_upload();
      $fileName = $_FILES['mobile_img']['name'];
      $images[] = $fileName;
  }
  $fileName = implode(',',$images);
  $this->my_model->upload_image($fileName);
}
private function set_upload_options()
{ 
// upload an image options
  $config = array();
  $config['upload_path'] = './upload/'; //give the path to upload the image in folder
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '0';
  $config['overwrite'] = FALSE;
  return $config;
}
}
?>

我的模特功能:

<?php
class My_Model extends CI_Model {
public function upload_image($fileName)
{
    if($filename!='' ){
     $filename1 = explode(',',$filename);
      foreach($filename1 as $file){
           $file_data = array(
              'name' => $file,
              'datetime' => date('Y-m-d h:i:s')
              );
           $this->db->insert('uploaded_files', $file_data);
    }
 }
 }
}
?>

答案 1 :(得分:0)

<?php echo form_open('admin/product_insert');?>
     <div class="row form-group">
        <div class="col-lg-2">
            Image 1
        </div>
        <div class="col-lg-5">
           <input type="file" class="form-control" name="photo1" required>
        </div>
    </div>
    <div class="row form-group">
       <div class="col-lg-2">
         Image 2
    </div>
    <div class="col-lg-5">
        <input type="file" class="form-control" name="photo2" Required>
   </div>
</div>
    <div class="row form-group">
        <div class="col-lg-2">
            Image 3
        </div>
        <div class="col-lg-5">
            <input type="file" class="form-control" name="photo3" Required>
        </div>
    </div>
<input type="submit">

答案 2 :(得分:0)

public function product_insert(){
                    $pro=$this->input->post('productName');
                    for($i=0;$i<=3;$i++){
                        $config['upload_path'] = './images/';
                        $config['allowed_types'] = 'gif|jpg|png';
                        $config['file_name']=$pro.$i;
                        $this->load->library('upload',$config);
                        $this->upload->do_upload('photo'.$i);
                        $this->upload->initialize($config);
                    }