繁体   English   中英

重命名文件上传代码点火器

[英]Rename file upload 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()
            )
        );
    }

现在我想要文件名作为上传的日期时间。我该怎么做?

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

将所有upload->initialize()代码移动到$config数组,这将使您更好地了解配置。

$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)
$config['file_name'] = date("Y-m-d_His") . '-' . $_FILES['userfile']['name'];

或者你可以使用

$config['encrypt_name'] = TRUE;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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