You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter文件上传重命名:如何将文件名设为上传时间?

Rename Uploaded Files to Upload Datetime with CodeIgniter Upload Class

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:

  1. encrypt_name is overriding your custom filename
    When encrypt_name is set to TRUE, CodeIgniter automatically generates a random encrypted filename and ignores the file_name value you defined. You need to set this to FALSE to use your custom naming logic.

  2. Invalid characters in your datetime format
    The format Y/m/d H:i:s includes 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_name to FALSE so your custom file_name is 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 like 2024-05-20...txt instead of the clean 2024-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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:35:55