codeigniter-带有图像和pdf上传的表格

时间:2018-08-12 04:36:14

标签: php codeigniter upload

我有一个可以接受一些文本输入字段和一个文件字段(用于上传图像和pdf)的表格。 我的问题是这样的,如果我没有填写上传图片,则会收到错误消息。如何通过函数使错误变为文本默认值,或者我的意思是我的函数$default_jarkom等导致空字段更改? 控制器为:

public function tambah() 
{
    //$file             = base_url()."assets/file/".$_FILES['berkas']['name'];
    $default_jarkom = base_url()."assets/images/img1.jpg";
    $default_android = base_url()."assets/images/img2.jpg";
    $file_gambar    = base_url()."assets/images/".$_FILES['berkas1']['name'];
    if (isset($_POST['nama']) || isset($_POST['deskripsi'])) {
        $matkul             = $_POST['matkul'];
        $dosen              = $_POST['dosen'];
        $nama               = $_POST['nama'];
        $deskripsi          = $_POST['deskripsi'];      
        $file               = $_FILES['berkas']['name'];
        $file_gambar;
        $check              = $this->db->query(
                                        "SELECT * FROM materi
                                        WHERE nama_materi='$nama' 
                                        OR deskripsi='$deskripsi';"
                                        );
        $msg                = false;
        if ($check->num_rows()==0){
            $id                 = $this->materi_model->buat_id();       
            $simpan             = $this->materi_model->tambah(
                                    $id,
                                    $matkul             = $_POST['matkul'],
                                    $dosen,
                                    $nama               = $_POST['nama'],
                                    $deskripsi          = $_POST['deskripsi'],
                                    $file               = $_FILES['berkas']['name'],
                                    $file_gambar
                                );

            if ($simpan) {
                $this->aksi_upload() ;
                $this->aksi_upload1() ;
                $msg        = true;
            }
        }           
        echo json_encode($msg);
    }
}

public function aksi_upload(){
    $config['upload_path']          = './assets/file/';
    $config['allowed_types']        = 'gif|jpg|png|pdf|doc';
    $config['max_size']             = 10000;


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

    if ( ! $this->upload->do_upload('berkas')){
        $error = array('error' => $this->upload->display_errors());
        //$this->load->view('data_tugas_mhs', $error);
        return $error;
    }else{
        $data = array('upload_data' => $this->upload->data());
        //$this->load->view('data_tugas_mhs', $data);
        return $data;
    }
}

public function aksi_upload1()
{
    $image = FALSE; //by default file is not uploaded
    $data = array();
    $config['upload_path']          = './assets/images/';
    $config['allowed_types']        = 'gif|jpg|png|pdf|doc';
    $config['max_size']             = 10000;


    $this->load->library('upload', $config);
    $default_jarkom = base_url()."assets/images/img1.jpg";
    if ( ! $this->upload->do_upload('berkas1'))
    {
        $error = array('error' => $this->upload->display_errors());     
        return $error;
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());      
        return $data;
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您应该先上传,然后再插入以获取正确的上传文件名。您还应该检查所有帖子,看看它们是否为空,否则可能会发生错误。而且,如果您的查询不安全,则应该转义输入或使用查询构建器。

我还修改了您的upload函数以使内容保持干燥。

public function tambah() {
    $default_jarkom = base_url() . "assets/images/img1.jpg";
    $default_android = base_url() . "assets/images/img2.jpg";
    $msg = false;
    if (isset($_POST['nama']) || isset($_POST['deskripsi'])) {
        $nama = $_POST['nama'];
        $deskripsi = $_POST['deskripsi'];
        $matkul = isset($_POST['matkul']) ? $_POST['matkul'] : '';
        $dosen = isset($_POST['dosen']) ? $_POST['dosen'] : '';
        // insecure! you aren't escaping! using query builder or escape your data!!!
        $this->db->where('nama_materi', $nama);
        $this->db->or_where('deskripsi', $deskripsi);
        if ($this->db->count_all_results('materi')) {
            $file = $this->aksi_upload('berkas', $default_jarkom);
            $file_gambar = $this->aksi_upload('berkas1', $default_jarkom);
            $id = $this->materi_model->buat_id();
            if ($this->materi_model->tambah($id, $matkul, $dosen, $nama, $deskripsi, $file, $file_gambar)) {
                $msg = true;
            }
        }
    }
    echo json_encode($msg);
}

/**
 * Upload function
 * 
 * @param string $field
 * @param string $default Failed upload, return this
 * @return string
 */
public function aksi_upload($field = 'userfile', $default = '') {
    $this->load->library('upload');
    $config['upload_path'] = './assets/file/';
    $config['allowed_types'] = 'gif|jpg|png|pdf|doc';
    $config['max_size'] = 10000;
    $this->upload->initialize($config, true);
    if (!$this->upload->do_upload($field)) {
        return $default;
    }
    return base_url() . "assets/images/" . $this->upload->data('file_name');
}