简体   繁体   中英

How do Insert / Save new file name after rename to my database?

How do we insert new file name after remaining in my database? I have file with same name but different image example jellyfish.jpg and jellyfish.jpg . After set 'overwrite' to set false and I try to upload jellyfish.jpg twice and successfully upload the image in first name jellyfish.jpg and the second upload is jellyfish1.jpg in my folder uploads. However, after I check in database, name of file is same jellyfish.jpg and jellyfish.jpg and not jellyfish.jpg and jellyfish1.jpg

How to insert new file name in my database?

Relevant controller code:

 <?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');}}}

The file to upload

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

*update (solved) I'm change to this code from

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

to

       $lampiran = $this->upload->file_name;

thanks to suggest editing my question and sorry for my lack english

Here's a simple function write inside the controller:

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;
  }
}

Call this function before $config array as

$newName = $this->incrementFileName( "uploads/", $_FILES["my_file"]["name"]);

In $config array add 'file_name'=>$newName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM