数据库中的图像名称仅是'nama_ktp',并且必须是两个名称,1是'foto_ktp',名称2是'foto_laporan'
控制器:Pelaporan.php
public function __construct()
{
parent::__construct();
$this->load->model('M_pelaporan','pelaporan');
}
public function index()
{
$data['konten']='pelaporan';
$this->load->view('template', $data);
}
public function tambah_laporan()
{
# code...
$foto_ktp = $_FILES['foto_ktp']['name'];
$foto_laporan = $_FILES['foto_laporan']['name'];
if($foto_ktp !== ""){
if(file_exists($upload_dir.$file_name)){
show_error('file already exist');
}
else{
$config['upload_path'] = './assets/img/';
$config['log_threshold'] = 1;
$config['allowed_types'] = 'jpeg|png|jpg';
$config['max_size'] = '10000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name']='foto_ktp';
$config['overwrite'] = false;
$this->load->library('upload',$config);
$this->upload->do_upload('foto_ktp');
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
}
}
if($foto_laporan !== ""){
if(file_exists($upload_dir.$file_name1)){
show_error('file already exist');
}
else{
$config['upload_path'] = './assets/img/';
$config['allowed_types'] = 'jpeg|png|jpg';
$config['max_size'] = '10000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name']='foto_laporan';
$config['overwrite'] = false;
$this->load->library('upload',$config);
$this->upload->do_upload('foto_laporan');
$upload_data = $this->upload->data();
$file_name1 = $a.$upload_data['file_name'];
}
}
$this->pelaporan->simpan_laporan($file_name,$file_name1,$foto_ktp,$foto_laporan);
redirect('pelaporan','refresh');
}
型号:M_pelaporan
class M_pelaporan extends CI_Model {
public function simpan_laporan($nama_file,$nama_file1)
{
if($nama_file==""){
$object = array(
'id_kasir'=>$this->session->userdata('id_kasir'),
'laporan'=>$this->input->post('laporan'),
'lok_laporan'=>$this->input->post('lok_laporan')
);
}else{
$object = array(
'id_kasir'=>$this->session->userdata('id_kasir'),
'laporan'=>$this->input->post('laporan'),
'foto_ktp'=>$nama_file,
'foto_laporan'=>$nama_file1,
'lok_laporan'=>$this->input->post('lok_laporan')
);
}
return $this->db->insert('laporan',$object);
}
查看:pelaporan.php
<form action="<?=base_url('index.php/Pelaporan/tambah_laporan')?>" method="POST" enctype="multipart/form-data">
<table class="table">
<tr>
<td>Laporan</td>
<td><input type="text" name="laporan" class="form-control"><br>
</tr>
<tr>
<td>Foto KTP</td>
<td><input type="file" name="foto_ktp" class="file" multiple="true"><br>
</tr>
<tr>
<td>Foto laporan</td>
<td><input type="file" name="foto_laporan" class="file" multiple="true"><br>
</tr>
<tr>
<td>Lokasi laporan</td>
<td><input type="text" name="lok_laporan" class="form-control"><br>
</tr>
<tr>
<td><input type="submit" name="simpan" value="simpan" class="btn btn-success"></td>
</tr>
</table>
</form>
此代码是上载2张图片,但第一个名为``foto_ktp''的图片始终会替换为上一张已上传的照片,应按示例foto_ktp1,foto_ktp2的顺序替换。另一个名为``foto_pelaporan''的输入图像可以上传图片,但上传时的名称为应该命名为“ foto_laporan”的“ foto_ktp”,还应带有序列号
答案 0 :(得分:0)
以此替换您的方法
public function tambah_laporan()
{
// Upload First Image -
$image_data1 = $this->uploadImage('foto_ktp');
$image1_name = isset($image_data1['file_name']) ? $image_data1['file_name'] : '';
// Upload Second Image - Same Conditions Applied As in the First Image
$image_data2 = $this->uploadImage('foto_laporan');
$image2_name= isset($image_data2['file_name']) ? $image_data2['file_name'] : '';
$this->pelaporan->simpan_laporan($image1_name,$image2_name);
redirect('pelaporan','refresh');
}
在Controller内添加此方法-此方法用于上传图像,如果您要..?
,它也可以返回图像验证错误。public function uploadImage($image_name)
{
$upload_dir = './assets/img/'; // directory path where your files/images are uploaded.
if($image_name !== ""){
if(file_exists($upload_dir.$image_name))
{
$data['error'] = 'Image Already Exists';
}
else
{
$config['upload_path'] = $upload_dir;
$config['allowed_types'] = 'jpeg|png|jpg';
$config['max_size'] = '10000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name']= $image_name;
$config['overwrite'] = false;
$this->load->library('upload',$config);
if($this->upload->do_upload($image_name)) {
$upload_data = $this->upload->data();
$data['file_name'] = $upload_data['file_name'];
}else{
$upload_data = $this->upload->data();
$data['error'] = $this->upload->display_errors();
}
}
return $data;
}
}
模型-在模型中替换它。
class M_pelaporan extends CI_Model {
public function simpan_laporan($image_file1,$image_file2)
{
// You Don't Need Image Validation Here You Can do this in the Controller
$object = array(
'id_kasir'=>$this->session->userdata('id_kasir'),
'laporan'=>$this->input->post('laporan'),
'foto_ktp'=>$image_file1,
'foto_laporan'=>$image_file2,
'lok_laporan'=>$this->input->post('lok_laporan')
);
return $this->db->insert('laporan',$object);
}
}
答案 1 :(得分:0)
编辑此代码。
$upload_data = $this->upload->data();
$file_name1 = $a.$upload_data['file_name'];
到
$upload_data2 = $this->upload->data();
$file_name1 = $upload_data2['file_name'];