重命名文件上传codeigniter

时间:2018-01-30 09:28:41

标签: php codeigniter

我使用codeigniter上传类上传文档文件。我需要重命名该文件。这是我的代码,

$doc_id=0;
    if(isset($_FILES['doc_file']['name'])){ 



 $config['file_name'] = date('Y/m/d H:i:s').pathinfo($_FILES["doc_file"]
 ['name'], PATHINFO_EXTENSION); 
 $config['upload_path'] = './uploads/';
 $config['overwrite'] = 'FALSE';
 $config['max_filename'] = '300';
 $config['encrypt_name'] = 'TRUE';
 $config['remove_spaces'] = 'TRUE';
 $config['allowed_types'] = '*';
 $config['max_size'] = $this->settings->info->file_size;


  $this->load->library('upload', $config);


        if ( ! $this->upload->do_upload('doc_file'))
        {
                $error = array('error' => $this->upload->display_errors());

                $this->template->jsonError(lang("error_152") . "<br /><br />" .
                     $this->upload->display_errors() . "<br />" . mime_content_type($_FILES['doc_file']['tmp_name']));
        }

                                $data = array('upload_data' => $this->upload->data());


        $doc_id = $this->feed_model->add_doc(array(


            "file_name" => $data['file_name'],
            "file_type" => $data['file_type'],
            "extension" => $data['file_ext'],
            "file_size" => $data['file_size'],

            "userid" => $this->user->info->ID,
            "timestamp" => time()
            )
        );
    }

现在我希望将文件名作为日期时间上传。我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

you can use this 

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|pdf';
    $config['max_size'] = 100;
    $config['max_width'] = 1024;
    $config['max_height'] = 768;
    $config['file_name'] =time().'-'.date("Y-m-d").'-'.$_FILES['userfile']['name'];
    $this->load->library('upload', $config);

    if (!$this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('welcome_message', $error);
    } else {



        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }

答案 1 :(得分:0)

将所有upload->initialize()代码移至 $config array ,这样可以让您更好地了解配置。

$newName = "my-file".date('Y/m/d H:i:s')".".pathinfo($_FILES["fileName"]['name'], PATHINFO_EXTENSION);

$config['upload_path'] = './uploads/';
$config['overwrite'] = '';
$config['max_filename'] = '';
$config['encrypt_name'] = '';
$config['remove_spaces'] = '';
$config['allowed_types'] = '';
$config['max_size'] = '';
$config['file_name'] = $newName; # add this

$this->load->library('upload', $config); # or $this->upload->initialize($config)

答案 2 :(得分:0)

$config['file_name'] = date("Y-m-d_His") . '-' . $_FILES['userfile']['name'];

或者您可以使用

$ config ['encrypt_name'] = TRUE;

在您的代码中。它将文件重命名为一些随机字符串