在codeigniter中上传多个图像

时间:2016-06-01 09:24:24

标签: php codeigniter

我创建了一个包含3 input(type["file"])的表单 Student_Image,Father_Image,Mother_Image

我想上传文件夹中的图像,并根据我的名字命名图像,并将该图像名称保存到数据库中。

5 个答案:

答案 0 :(得分:0)

$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);

$data = [];
$errors = [];
foreach ($this->input->post('files') as $file_name)
{
    if ($this->upload->do_upload($file_name))
    {
        $data[$file_name] = $this->upload->data();
    }
    else 
    {
        $errors[$file_name] = $this->upload->display_errors();
    }
}

答案 1 :(得分:0)

使用以下功能

function upload_image()
{
$img1 = $this->_upload_image('Student_Image','YOUR_FOLDER_PATH','jpg|png|jpeg|gif',3000);
if($img1['status']){$filename1 = $img1['filename'];}
$img2 = $this->_upload_image('Father_Image','YOUR_FOLDER_PATH','jpg|png|jpeg|gif',3000);
if($img2['status']){$filename2 = $img2['filename'];}

$img3 = $this->_upload_image('Mother_Image','YOUR_FOLDER_PATH','jpg|png|jpeg|gif',3000);

if($img3['status']){$filename3 = $img3['filename'];}
}

function _upload_image($userfile,$image_path,$allowed,$max_size)
{
    if($_FILES[$userfile]['name']!='')
    {
        if(!is_dir($image_path))
        {
            mkdir($image_path);
        }
        $config['upload_path'] = $image_path;
        $config['allowed_types'] = $allowed;
        $config['max_size'] = $max_size;
        $img=$_FILES[$userfile]['name'];
        $random_digit=rand(00,99999); //here you can change file name 
        $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1));
        $file_name=$random_digit.$ext;
        $config['file_name'] = $file_name;
        $this->ci->load->library('upload', $config);

        if($this->ci->upload->do_upload($userfile))
        {
            return array('status'=>TRUE,'filename'=>$this->ci->upload->file_name);
        }
        else {return array('status'=>FALSE,'error'=>$this->ci->upload->display_errors('<span>','</span>'));}
    }
}

答案 2 :(得分:0)

如果您的控制器应该是这样的话,您必须在时间上添加多个图像

public function upload_image($table_id){
        $this->data['table_id'] = $table_id;
        $this->data['data'] = $this->your_model->get($table_id);

        if($this->input->post()) {
            $upload_image = true;
            $upload_path = FCPATH.'/uploads/upload_image';
            $uploadedImageName = array();
            for ($i=1;$i<=5;$i++){
                $field_name ='image'.$i;
                $temp_file_names = $this->file[$field_name]['name'];
                if(isset($temp_file_names) && $temp_file_names!=''){
                    $file_name = time().'_'.$this->randomString(10).'.'.$this->getFileExtension($temp_file_names);
                    if(!$this->uploadImage($upload_path,$file_name,$field_name)){
                        $this->session->set_flashdata('error', $this->file_error);
                    } else {
                        $uploadedImageName[] = $this->file_data['file_name'];
                    }
                } else {
                    $uploadedImageName[] = $this->input->post('old_image'.$i);
                }
            }
            $update_data = array('Student_Image'=>json_encode($uploadedImageName));
            if(!$this->your_model->updateimage($table_id,$update_data)){
                $this->session->set_flashdata('error', 'Record couldn\'n updated. Please try again.');
            } else {
                $this->session->set_flashdata('success', 'Update successfully.');
                redirect('/upload_image/'.$table_id);
            }
        }
        $this->load->view('/upload_image',$this->data);
    }

并 模型

public function updateimage($table_id,$data){
        $sql ="update tablename set ";
        $update_data = array();
        if(empty($data)){
            return false;
        }
public function get($table_id){
        $sql ="select * from tablename where table_id = ?";
        $rs = $this->db->query($sql,array($table_id));
        $record = $rs->result();
        return (array)$record[0];
    }

答案 3 :(得分:0)

你可以这样尝试

        $errors= array();
        foreach($_FILES['gallery']['tmp_name'] as $key => $tmp_name ){
            $file_name = $key.$_FILES['gallery']['name'][$key];
            $file_size =$_FILES['gallery']['size'][$key];
            $file_tmp =$_FILES['gallery']['tmp_name'][$key];
            $file_type=$_FILES['gallery']['type'][$key];    

            if($file_size > 2097152){
                $errors[]='File size must be less than 2 MB';
            }       

            $desired_dir="assets/images/products/";
            $array=array('pid'=>$insert_id,'imagepath'=>$desired_dir.$file_name);
            if(empty($errors)==true){

                    if(is_dir($desired_dir)==false){
                        mkdir("$desired_dir", 0700);        // Create directory if it does not exist
                    }
                    if(is_dir("$desired_dir/".$file_name)==false){
                        move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
                }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
                }
                $this->db->insert('mx_products_images',$array);             
            }
        }

答案 4 :(得分:0)

<?php echo form_open_multipart('upload_controller/do_upload');?>
<div class="profilethumb">
<h4>Student's Image</h4>
<input type="file" name="S_image" id="image" class="btn btn-primary">
</div><!--profilethumb-->
<div class="profilethumb">
<h4>Father's Image</h4>
<br><input type="file" name="F_image" class="btn-primary"><br>
</div><!--profilethumb-->
<div class="profilethumb">
<h4>Mother's Image</h4>
<br><input type="file" name="M_image" class="btn-primary"><br>
</div><!--profilethumb-->
<div class="profilethumb">
<h4>Local Guardian Image</h4>
<br><input type="file" name="LG_image" class="btn-primary"><br>
</div><!--profilethumb-->
<input type="submit" value="submit detail" class="btn-primary" />
<?php echo "</form>"?>

这是我的From详细信息..将图片上传到上传文件夹,并将图片名称保存到S_Info表中。