繁体   English   中英

使用 CodeIgniter 使用两个输入字段仅上传两个图像

[英]Upload only Two Image using two input field Using CodeIgniter

控制器

public function insertDepartmentImage()
{
    $this->load->model('Gallery_Model/Gallery_Model');  
    $config['upload_path'] = './uploads/galleryImg';
    $config['allowed_types'] = 'png|jpg|gif|jpeg';
            $config['max_size']    = '10000000';             
    $config['file_name']='smallImage';
    $config['file_name1']='bigImage';
    $config['overwrite']=false;   
    $this->load->library('upload', $config);        
    if (!is_dir('uploads'))
    {
        mkdir('./uploads', 0777, true);
    }
    $dir_exist = true;  
    if (!is_dir('uploads/' . $album))
    {
    mkdir('./uploads/' . $album, 0777, true);
    $dir_exist = false; // dir not exist
    }
    else{ }              

    if (!$this->upload->do_upload('filename'))
    {
        $this->load->model('Gallery_Model/Gallery_Model');            
        $upload_error = array('error' => $this->upload->display_errors());       
        $this->load->view('/admin/Gallery/vwGallery',$upload_error);
    }
    else
    {   
        $upload_data = $this->upload->data();   
        $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';        
        $this->load->model('Gallery_Model/Gallery_Model');               
        $file_name =   $upload_data['file_name'];
        $file_name1 =   $upload_data['file_name1'];
        $deptId = $this->input->post('deptId'); 
        $deptDetails = $this->input->post('deptDetails');
        $deptDetails1 = $this->input->post('deptDetails1');      
        $this->Gallery_Model->insertDepartmentImage($deptId,$file_name,$file_name1,$deptDetails,$deptDetails1);           
    }       
} 

它工作正常,但只将一个文件名上传到数据库中。 我必须再上传一张图片到数据库中。 但它不工作。上传另一个图像为此做什么。

在我看来,我给了两个输入字段<input id="file-0" name="filename" class="file" type="file" multiple="true" />

<input id="file-0" name="filename1" class="file" type="file" multiple="true" />

谢谢你们。 但是我只会将代码更改为 Controller 并且它对我有用。

public function insertDepartmentImage()
{
     $m = $_FILES['filename']['name'];      
     $n = $_FILES['filename1']['name']; 
    if ($m !== "")
    {
        $config['upload_path'] = './uploads/galleryImg';                        
        $config['log_threshold'] = 1;
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '100000'; // 0 = no file size limit
        $config['file_name']='smallImage';          
        $config['overwrite'] = false;
        $this->load->library('upload', $config);
         $this->upload->do_upload('filename');
         $upload_data = $this->upload->data();
         $file_name = $upload_data['file_name'];
 }          
 if ($n !== "")
    {
        $config['file_name']='bigImage';
        $config['upload_path'] = './uploads/galleryImg';             
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '100000'; // 0 = no file size limit           
        $config['overwrite'] = false;
        $this->load->library('upload', $config);
        $this->upload->do_upload('filename1');
        $upload_data = $this->upload->data();
        $file_name1 = $upload_data['file_name']; 
    } 


        $deptId = $this->input->post('deptId'); 
        $deptDetails = $this->input->post('deptDetails');
        $deptDetails1 = $this->input->post('deptDetails1');

        $this->Gallery_Model->insertDepartmentImage($deptId,$file_name,$file_name1,$deptDetails,$deptDetails1); 

}/* closed insertDepartmentImage*/      

看法

<input id="file-0" name="filename" class="file" type="file" multiple="true" />
<input id="file-0" name="filename1" class="file" type="file" multiple="true" />

默认情况下,codeIgniter 不支持多文件上传。 所以你可以使用这个库 CodeIgniter Multiple Upload Library

https://github.com/anuragrath/CodeIgniter-Multiple-File-Upload

谢谢你们...我现在解决了我的问题...

这是我的代码,希望它可以帮助

public function dashboard_templatedefault(){

    $upload1 = $_FILES['upload1']['name'];
    $upload2 = $_FILES['upload2']['name'];
    $upload3 = $_FILES['upload3']['name'];
    $upload4 = $_FILES['upload4']['name'];
    $upload5 = $_FILES['upload5']['name'];
    $upload6 = $_FILES['upload6']['name'];
    $upload7 = $_FILES['upload7']['name'];
    $upload8 = $_FILES['upload8']['name'];

    if($upload1 !== ""){

        $config['upload_path'] = './upload/';                        
        $config['log_threshold'] = 1;
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '0'; // 0 = no file size limit
        $config['file_name']='upload1';          
        $config['overwrite'] = false;
         $this->load->library('upload', $config);
         $this->upload->do_upload('upload1');
         $upload_data = $this->upload->data();
         $file_name = $upload_data['file_name'];

    }
    if($upload2 !== ""){

        $config['file_name']='upload2';
        $config['upload_path'] = './upload/';             
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] = '0'; // 0 = no file size limit           
        $config['overwrite'] = false;
        $this->load->library('upload', $config);
        $this->upload->do_upload('upload2');
        $upload_data = $this->upload->data();
        $file_name1 = $upload_data['file_name'];

    }
    if($upload3 !== ""){

        $config['file_name']='upload3';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] ='jpg|png|jpeg|gif';
        $config['max_size'] = '0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload3');
        $upload_data = $this->upload->data();
        $file_name2 = $upload_data['file_name'];

    }
    if($upload4 !== ""){

        $config['file_name'] = 'upload4';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload4');
        $upload_data = $this->upload->data();
        $file_name3 = $upload_data['file_name'];

    }
    if($upload5 !== ""){

        $config['file_name'] = 'upload5';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload5');
        $upload_data = $this->upload->data();
        $file_name4 = $upload_data['file_name'];

    }
    if($upload6 !== ""){

        $config['file_name'] = 'upload6';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload6');
        $upload_data = $this->upload->data();
        $file_name5 = $upload_data['file_name'];

    }
    if($upload7 !== ""){

        $config['file_name'] = 'upload7';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload7');
        $upload_data = $this->upload->data();
        $file_name6 = $upload_data['file_name'];

    }
    if($upload8 !== ""){

        $config['file_name'] = 'upload8';
        $config['upload_path'] ='./upload/';
        $config['allowed_types'] = 'jpg|png|jpeg|gif';
        $config['max_size'] ='0';
        $config['overwrite'] = false;
        $this->load->library('upload',$config);
        $this->upload->do_upload('upload8');
        $upload_data = $this->upload->data();
        $file_name7 = $upload_data['file_name'];

    }



    $this->form_validation->set_rules('name','','xss_clean');
    $this->form_validation->set_rules('occasion','','xss_clean');
    $this->form_validation->set_rules('party_date','','xss_clean');
    $this->form_validation->set_rules('location','','xss_clean');
    $this->form_validation->set_rules('upload1','','xss_clean');
    $this->form_validation->set_rules('upload2','','xss_clean');
    $this->form_validation->set_rules('upload3','','xss_clean');
    $this->form_validation->set_rules('upload4','','xss_clean');
    $this->form_validation->set_rules('upload5','','xss_clean');
    $this->form_validation->set_rules('upload6','','xss_clean');
    $this->form_validation->set_rules('upload7','','xss_clean');
    $this->form_validation->set_rules('upload8','','xss_clean');
        $this->form_validation->run();

            $template_name = $this->input->post('template_name');
            $name = $this->input->post('name');
            $occasion = $this->input->post('occasion');
            $party_date = $this->input->post('party_date');
            $location = $this->input->post('location');
            $created = date('h:m:s a m/d/y');
            $profile_id = $this->input->post('profile_id');
            $this->admin_model->dashboard_templatedefault($template_name,$name,$occasion,$party_date,$location,$created,$profile_id,$upload1,$upload2,$upload3,$upload4,$upload5,$upload6,$upload7,$upload8);



}

你的代码似乎太长了试试这个

 public function insertDepartmentImage()
    {
         $upload1 = $_FILES['upload1']['name'];      
         $upload2 = $_FILES['upload2']['name'];

        $config = array(
                'upload_path' => './uploads/galleryImg/',
                'log_threshold' => 1,
                'allowed_types' => 'jpg|png|jpeg|gif',
                'max_size' => '0',
                'overwrite' => false
            );
    for($i = 1; $i<=2; $i++){
        if(!empty('$upload'.$i)){
                        $config['file_name']='upload'.$i;
                        $this->load->library('upload', $config);
                        $this->upload->do_upload('upload'.$i);
                        $upload_data = $this->upload->data();
                        $file_name = $upload_data['file_name'];
                    }
    }
        $deptId = $this->input->post('deptId'); 
        $deptDetails = $this->input->post('deptDetails');
        $deptDetails1 = $this->input->post('deptDetails1');

        $this->Gallery_Model->insertDepartmentImage($deptId,$file_name,$file_name1,$deptDetails,$deptDetails1);



    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM