保留在数据库中后如何插入新文件名?
我有相同名称的文件,但图像示例jellyfish.jpgjellyfish.jpg不同。
将'overwrite'设置为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;


感谢建议编辑我的问题,对不起我的英语不好

最佳答案

这是在控制器内部编写的简单函数:

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数组之前调用此函数为

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


$config数组中添加'file_name'=>$newName

关于php - 重命名到数据库后如何插入/保存新文件名?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28604588/

10-10 11:14