如何在保留数据库后插入新文件名?
我的文件名相同,但图片示例jellyfish.jpg
和jellyfish.jpg
不同。
设置'覆盖'设置为false,我尝试上传jellyfish.jpg
两次并成功上传
在我的文件夹上传中,名字jellyfish.jpg
中的图像和第二次上传是jellyfish1.jpg
。
但是,在我签入数据库后,文件名是jellyfish.jpg
和jellyfish.jpg
而不是jellyfish.jpg
和jellyfish1.jpg
如何在我的数据库中插入新文件名?
相关控制器代码:
<?php
class Cutidiluar extends CI_Controller {
var $limit=10;
var $offset=10;
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('Cutidiluar_Model');
}
public function add()
{ if($this->session->userdata('LOGIN')=='TRUE')
{
$this->load->library('form_validation');
$this->load->model('Diluartahunan_Model');
$this->form_validation->set_rules('ket', 'ket');
$this->form_validation->set_rules('lama', 'lama');
$this->form_validation->set_rules('lampiran', 'lampiran');
if ($this->form_validation->run() == false) {
$data['nama'] = $this->Diluartahunan_Model->nama();
$data['view'] = 'Cutidiluar/add';
$data['judul']='';
$this->load->view('index',$data);
}else {
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => false,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload',$config);
$this->upload->do_upload();
$this->upload->data();
//file berhasil diupload -> lanjutkan ke query insert
$npp=$this->session->userdata('NPP');
$id_jenis = $_POST['id_jenis'];
$ket = $_POST['ket'];
$lama = $_POST['lama'];
$tgl_selesai= date('Y-m-d', strtotime($_POST['tgl_selesai']));
$tgl_mulai= date('Y-m-d', strtotime($_POST['tgl_mulai']));
$lampiran = $_FILES['userfile']['name'];
$data = array (
'npp'=> $npp,
'id_jenis'=> $id_jenis,
'tgl_pengajuan' => date('y-m-d'),
'ket' => $ket,
'status' => 'P',
'lampiran'=> $lampiran,
'lama'=> $lama,
'tgl_mulai'=> $tgl_mulai,
'tgl_selesai'=> $tgl_selesai,
);
$this->Cutidiluar_Model->add($data);
redirect('Cutidiluar');}}}
要上传的文件
$lampiran = $_FILES['userfile']['name'];
*更新(已解决) 我改变了这段代码 从
$lampiran = $_FILES['userfile']['name'];
到
$lampiran = $this->upload->file_name;
感谢建议编辑我的问题并抱歉我缺乏英语
答案 0 :(得分:0)
这是控制器内部的简单函数写入:
function incrementFileName($file_path,$filename){
if(count(glob($file_path.$filename))>0)
{
$file_ext = end(explode(".", $filename));
$file_name = str_replace(('.'.$file_ext),"",$filename);
$newfilename = $file_name.'_'.count(glob($file_path."$file_name*.$file_ext")).'.'.$file_ext;
return $newfilename;
}
else
{
return $filename;
}
}
在$ config array之前调用此函数
$newName = $this->incrementFileName( "uploads/", $_FILES["my_file"]["name"]);
在$config
数组中添加'file_name'=>$newName