【问题标题】:Upload Codeigniter [duplicate]上传 Codeigniter [重复]
【发布时间】:2016-07-22 17:13:01
【问题描述】:

这是我的代码,我的照片无法插入数据库。我不知道问题出在哪里。

实际上,我想使用 codeigniter 进行在线考试。我想用图片上传问题。但是当我尝试上传图片时,代码不起作用。

但问题成功插入数据库。只有图片上传失败

控制器:

function insert(){
	$nama_asli = $_FILES['userfile']['name'];
	$config ['file_name'] = $nama_asli;
	$config ['upload_path'] = './images/';
	$config ['allowed_types'] = 'gif|jpg|png|jpeg';
	$config ['max_size'] = '2500';
	$config ['max_width']  = '2600';
	$config ['max_height']  = '2200';
		
	
	$id_soal = '';
	$soal = $_POST['soal'];
	$a = $_POST['a'];
	$b = $_POST['b'];
	$c = $_POST['c'];
	$d = $_POST['d'];
	$kunci = $_POST['kunci'];
	$status = $_POST['status'];

	
	$this->load->library('upload', $config);
	
	if ( ! $this->upload->do_upload()){
			$error       = array('error' => $this->upload->display_errors());
			$this->load->view('admin/home/create_admin', $error);
	}
	
	else{
	$data = array('upload_data' => $this->upload->data());
	$upload_data = $this->upload->data();
	$file_name = $upload_data['file_name'];	
	$data = array(
		'id_soal' => $id_soal,
		'soal' => $soal,
		'a' => $a,
		'b' => $b,
		'c' => $c,
		'd' => $d,
		'kunci' => $kunci,
		'status' => $status,
		'foto' => $file_name,
		);
	$hasil = $this->soal_model->Simpan('soal', $data);
	if($hasil>=1){
		redirect('dashboard/index', $data);
	}
	}
	
}

型号:

class Soal_model extends Ci_Model {

  public function Ambil($where= "") {
    $data = $this->db->query('select * from soal '.$where);
    return $data;
  }
  
  public function Simpan($tabel, $data){
    $res = $this->db->insert($tabel, $data);
    return $res;
  }

查看:

<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data">
<form class="form-horizontal" method="post" style = "margin : 10px;">
		<div class = "row">
		<div class = "col-sm-offset-3 col-sm-6">
		<div class="form-group">
		<label>Soal :</label>
		<textarea type="text" class="form-control" name="soal" id="soal" required></textarea>
		</div>
		
		<div class="form-group">
		<label>Jawaban A :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="a" id="a" required/>
		</div>
		
		<div class="form-group">
		<label>Jawaban B :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="b" id="b" required/>
		</div>
		
		<div class="form-group">
		<label>Jawaban C :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="c" id="c" required/>
		</div>
		
		<div class="form-group">
		<label>Jawaban D :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="d" id="d" required/>
		</div>
		
		<div class="form-group">	
		<label>Kunci :</label>
		<select name="kunci" id="kunci" class="form-control">
		<option>Select</option> 
		<option>A</option>  
		<option>B</option> 
		<option>C</option>
		<option>D</option>
		</select> 
		</div>
		
		<div class="form-group">	
		<label>Status :</label>
		<select name="status" id="status" class="form-control">
		<option value="">Select</option> 
		<option value="tampil">Tampil</option>  
		<option value="tidak">Tidak</option> 
		</select> 
		
		<div class="form-group">	
		<label>Photo :</label>
		<input type="file" name="foto" id="foto" size="20"/>
		</div>
		<br>
		
		<div class="form-group">	
        <button type="submit" class="btn btn-default">Simpan</button>
		<button type="reset" class="btn btn-default">Hapus</button>
		</div>
		</div>
		</div>
	</form>
	
 

数据库:

有人可以帮忙吗?

【问题讨论】:

  • 看起来没问题,试试把$config ['upload_path'] = './images';改成$config ['upload_path'] = './images/';
  • 没有结果。和以前一样。图片尚未上传
  • 您使用的是 2 个表单吗?您的视图文件中的表单中有表单
  • 我的错误......但现在我已经修复了该表单

标签: php html database codeigniter web


【解决方案1】:

需要添加upload::library的do_upload方法

$nama_asli                = $_FILES['userfile']['name'];

$config ['file_name']     = $nama_asli;
$config ['upload_path']   = './images/';
$config ['allowed_types'] = 'gif|jpg|png|jpeg';
$config ['max_size']      = '2500';

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

//you need to add this do_upload();
if ( ! $this->upload->do_upload('userfile')){
    $error       = array('error' => $this->upload->display_errors());

    $this->load->view('upload_form', $error);
}
else{
    $data        = array('upload_data' => $this->upload->data());
    $upload_data = $this->upload->data();
    $file_name   = $this->upload->file_name;
    .
    .
    .
    ...
}

【讨论】:

  • 我已经更新了我的控制器。请检查我的新更新。谢谢之前
  • 现在,你有什么错误或者问题已经解决了吗?
  • 其实问题并没有解决。你认为我的新控制器有错误的模式吗?
  • 退出$config ['file_name'] = $nama_asli;
  • 没有差异
猜你喜欢
  • 2018-08-17
  • 1970-01-01
  • 2020-06-12
  • 2015-07-25
  • 1970-01-01
  • 2018-04-03
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多