【问题标题】:Rename file upload codeigniter重命名文件上传codeigniter
【发布时间】:2018-01-30 09:28:40
【问题描述】:

我正在使用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()
    ));
}

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

【问题讨论】:

  • $this-&gt;upload-&gt;initialize(array( "upload_path" =&gt; './uploads/', "file_name" =&gt; 'time().$_FILES['doc_file']['name']', "overwrite" =&gt; FALSE, "max_filename" =&gt; 300, "encrypt_name" =&gt; TRUE, "remove_spaces" =&gt; TRUE, "allowed_types" =&gt; "*", "max_size" =&gt; $this-&gt;settings-&gt;info-&gt;file_size, ) );这个对吗?

标签: php codeigniter


【解决方案1】:

你可以用这个

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

【讨论】:

  • 移动到像A problem was encountered while attempting to move the uploaded file to the final destination这样的上传目录时显示错误
  • 你有没有给出上传文件的目的地,比如 $config['upload_path'] = './uploads/';
  • 使用上面的代码它工作正常,只需检查你犯错的地方
【解决方案2】:

将所有upload-&gt;initialize() 代码移至$config array,这将使您更好地了解配置。

$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)

【讨论】:

  • 给出错误&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A problem was encountered while attempting to move the uploaded file to the final destination.&lt;/p&gt;&lt;br /&gt;application/
  • 从未上传
  • 然后授予777上传目录的权限
【解决方案3】:
$config['file_name'] = date("Y-m-d_His") . '-' . $_FILES['userfile']['name'];

或者你可以使用

$config['encrypt_name'] = TRUE;

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

【讨论】:

  • 请在您的代码中添加一些解释,以便其他人可以从中学习
  • @Tassadaq Hussain 给出的答案也是正确的
猜你喜欢
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2012-03-06
  • 2012-06-24
相关资源
最近更新 更多