CodeIgniter文件上传重命名:如何将文件名设为上传时间?
I'm using CodeIgniter's Upload class to upload document files and need to rename the files to the datetime when they're uploaded. Here's my current code:
$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() )); }How can I set the filename to the datetime when the file is uploaded?
Let's fix this step by step - your current code has two critical issues that are blocking your custom datetime filename from working:
encrypt_nameis overriding your custom filename
Whenencrypt_nameis set toTRUE, CodeIgniter automatically generates a random encrypted filename and ignores thefile_namevalue you defined. You need to set this toFALSEto use your custom naming logic.Invalid characters in your datetime format
The formatY/m/d H:i:sincludes slashes (/) and spaces, which are invalid characters for filenames on most operating systems. This would either cause the upload to fail or create unexpected subdirectories (from the slashes). Replace these with safe separators like hyphens (-) and underscores (_).
Here's the corrected code with explanations:
$doc_id = 0; if(isset($_FILES['doc_file']['name'])) { // Extract file extension first, then create a filename-safe datetime string $file_ext = pathinfo($_FILES["doc_file"]['name'], PATHINFO_EXTENSION); $config['file_name'] = date('Y-m-d_H-i-s') . '.' . $file_ext; // Add a dot before the extension $config['upload_path'] = './uploads/'; $config['overwrite'] = FALSE; // Use boolean instead of string 'FALSE' for consistency $config['max_filename'] = 300; // Use integer instead of string '300' $config['encrypt_name'] = FALSE; // Disable encryption to enable custom filename $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() )); }
Key improvements:
- Changed
encrypt_nametoFALSEso your customfile_nameis respected - Updated the datetime format to
Y-m-d_H-i-s(fully compatible with all OS filesystems) - Fixed extension handling: added a dot (
.) between the datetime string and file extension (your original code would have produced something like2024-05-20...txtinstead of the clean2024-05-20_14-30-00.txt) - Swapped string values like
'FALSE'and'300'for proper booleans/integers (matches CodeIgniter's expected configuration types)
This will generate unique, descriptive filenames like 2024-05-20_14-30-00.pdf or 2024-05-20_15-45-32.docx that perfectly reflect the upload datetime.
内容的提问来源于stack exchange,提问作者usertest

