无法将图像和pdf与codeigniter

时间:2018-01-30 07:19:55

标签: php codeigniter

如何上传图像文件和pdf数据,我发现很难,因为我在不同的扩展中做了简化,但数据没有进入显示的文件夹..

function add_module_external(){
        $id_module = "9090909";
        $file_data = array();
        $image_data = array();

        $config_image["file_name"] = $id_module;
        $config_image["upload_path"] = "./assets/file/image-module/";
        $config_image["overwrite"] = TRUE;
        $config_image["allowed_types"] = "gif|jpg|png|";

        $this->load->library('upload', $config_image); 
        $this->upload->initialize($config_image);
        $upload_background = $this->upload->do_upload('other_image');
        if($upload_background){
            $image_data = $this->upload->data();
            $image = $image_data['file_name'];
        }else{
            $image = $this->input->post('background');
        }


        // pdf and ppt upload       
        $config_file['file_name'] = $id_module;
        $config_file['upload_path'] = '/assets/file/materi-module/';
        $config_file['overwrite'] = TRUE;
        $config_file['allowed_types'] = 'pdf';
        $this->load->library('upload', $config_file); 
        $this->upload->initialize($config_file);
        $upload_pdf = $this->upload->do_upload('pdf_data'); 

        if($upload_pdf){
                $file_data = $this->upload->data();
                $content = $file_data["file_name"];         
            }else{
                $content = $this->input->post("text_data");
            }   


        $query = $this->my_module->new_module($image, $id_module, $content);
                redirect($this->agent->referrer());     

    }

2 个答案:

答案 0 :(得分:1)

第二次加载上传库时,由于您已经加载了库,因此将跳过它,因此文件配置仍会在图像上传时加载。
你能做什么:
1.使用提供的图像配置来加载库 2.处理图像上传数据
3.使用文件配置初始化上载
4.处理文件上传数据

function add_module_external(){
    $id_module = "9090909";
    $file_data = array();
    $image_data = array();

    $config_image["file_name"] = $id_module;
    $config_image["upload_path"] = "./assets/file/image-module/";
    $config_image["overwrite"] = TRUE;
    $config_image["allowed_types"] = "gif|jpg|png|";

    $this->load->library('upload', $config_image); 
    $upload_background = $this->upload->do_upload('other_image');
    if($upload_background){
        $image_data = $this->upload->data();
        $image = $image_data['file_name'];
    }else{
        $image = $this->input->post('background');
    }


    // pdf and ppt upload       
    $config_file['file_name'] = $id_module;
    $config_file['upload_path'] = '/assets/file/materi-module/';
    $config_file['overwrite'] = TRUE;
    $config_file['allowed_types'] = 'pdf';
    $this->upload->initialize($config_file);
    $upload_pdf = $this->upload->do_upload('pdf_data'); 

    if($upload_pdf){
            $file_data = $this->upload->data();
            $content = $file_data["file_name"];         
        }else{
            $content = $this->input->post("text_data");
        }   


    $query = $this->my_module->new_module($image, $id_module, $content);
            redirect($this->agent->referrer());     

}

答案 1 :(得分:0)

您可以同时上传jpg和pdf文件,但不能重命名文件名

尝试此代码。

if(!empty($_FILES["ktp"])) {
    if(!file_exists("./uploads/".$this->input->post('no_hp'))) {
        mkdir("./uploads/".$this->input->post('no_hp'));
    }
    $configktp['upload_path']          = './uploads/'.$this->input->post('no_hp')."/";
    $configktp['allowed_types']        = '*';
    $this->load->library('upload', $configktp);
    if ( ! $this->upload->do_upload('ktp'))
    {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
    }

}

if(!empty($_FILES["ijazah"])) {
    $configijazah['upload_path']          = './uploads/'.$this->input->post('no_hp')."/";
    $configijazah['allowed_types']        = '*';
    $this->load->library('upload', $configijazah);
    if ( ! $this->upload->do_upload('ijazah'))
    {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
    }
}

if(!empty($_FILES["cv"])) {
    $configpdf['upload_path']          = './uploads/'.$this->input->post('no_hp')."/";
    $configpdf['allowed_types']        = '*';
    $this->load->library('upload', $configpdf);
    if ( ! $this->upload->do_upload('cv'))
    {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
    }
}