重命名后如何插入/保存新文件名到我的数据库?

时间:2015-02-19 10:59:29

标签: php mysql codeigniter

如何在保留数据库后插入新文件名? 我的文件名相同,但图片示例jellyfish.jpgjellyfish.jpg不同。 设置'覆盖'设置为false,我尝试上传jellyfish.jpg两次并成功上传 在我的文件夹上传中,名字jellyfish.jpg中的图像和第二次上传是jellyfish1.jpg。 但是,在我签入数据库后,文件名是jellyfish.jpgjellyfish.jpg而不是jellyfish.jpgjellyfish1.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;

感谢建议编辑我的问题并抱歉我缺乏英语

1 个答案:

答案 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