我有一个接受一些文本输入字段和一个文件字段(用于上传图像和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;
}
}
首先,您应该在插入之前上传以获得正确的上传文件名。您还应该检查所有帖子,看看它们是否为空,否则可能会发生错误。您应该转义输入或使用查询构建器,因为您有一个不安全的查询。
我也修改了你的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');
}