Php 重命名文件上载codeigniter
我正在使用codeigniter上载类上载文档文件。我需要重命名该文件。这是我的代码Php 重命名文件上载codeigniter,php,codeigniter,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/'; $
$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()
)
);
}
$doc\u id=0;
如果(isset($_FILES['doc_file']['name']){
$config['file_name']=日期('Y/m/d H:i:s')。路径信息($_FILES[“doc_file”]
['name'],路径信息_扩展名);
$config['upload_path']='./uploads/';
$config['overwrite']='FALSE';
$config['max_filename']='300';
$config['encrypt_name']='TRUE';
$config['remove_spaces']='TRUE';
$config['allowed_types']='*';
$config['max\u size']=$this->settings->info->file\u size;
$this->load->library('upload',$config);
如果(!$this->upload->do_upload('doc_file'))
{
$error=array('error'=>$this->upload->display_errors());
$this->template->jsonError(lang(“error_152”)。“
”。
$this->upload->display_errors()。“
”。mime_内容类型($_FILES['doc_file']['tmp_name']);
}
$data=array('upload_data'=>$this->upload->data());
$doc\u id=$this->feed\u model->add\u doc(数组)(
“文件名”=>$data[“文件名”],
“文件类型”=>$data[“文件类型”],
“扩展名”=>$data['file\u ext'],
“文件大小”=>$data[“文件大小”],
“userid”=>this->user->info->ID,
“时间戳”=>time()
)
);
}
现在我想要文件名作为上载的日期。我如何才能做到这一点?将所有
upload->initialize()
code移到这将使您更好地了解配置
$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['encrypt_name']=TRUE
在你的代码中。它会将文件重命名为一些随机字符串看看:
$this->upload->initialize(数组(“upload\u path”=>。/uploads/”,“file\u name”=>“time()。$\u FILES['doc\u file']['name'],“overwrite”=>FALSE,“max\u filename”=>300,“encrypt\u name”=>TRUE,“remove\u spaces”=>TRUE,“allowed\u type”=>“*”,“最大大小”=>$this->settings->info->file\u size,)
此选项正确吗?给出错误
尝试将上载的文件移动到最终目标时遇到问题。
application/
在上载之前从未获得上载目录的权限。如果移动到上载目录,则会显示错误,如尝试时遇到问题要将上传的文件移动到最终目的地
是否指定了上传文件的目的地,如$config['upload_path']='./uploads/';使用上面的代码可以很好地工作,只需检查您犯的错误。请在代码中添加一些解释,以便其他人可以从中学习。添加了解释。@Tassadaq Hussain给出的答案也是正确的
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);
}
$config['file_name'] = date("Y-m-d_His") . '-' . $_FILES['userfile']['name'];