[英]Rename file upload codeigniter
I'm using codeigniter upload class to upload document files.I need to rename the file.Here is my code,我正在使用 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()
)
);
}
Now I wants the file name as datetime it's uploaded.How can I done this?现在我想要文件名作为上传的日期时间。我该怎么做?
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);
}
Move all upload->initialize()
code to $config
array this will give you a better understanding of configurations.将所有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'];
or you can use或者你可以使用
$config['encrypt_name'] = TRUE; $config['encrypt_name'] = TRUE;
in your code.在你的代码中。 It will rename file to some random string它将文件重命名为一些随机字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.