使用do_upload函数上传i文件时文件名更改

时间:2018-01-09 07:24:20

标签: php

当我在代码点火器中使用do_upload()上传文件时,它会上传文件,但会在空格的位置用下划线(_)重命名。 要上传的文件名:A B C.pdf 上传的文件:A_B_C.pdf

控制器中的代码上传文件:

//$filefeild is the name of folder in which file is to be saved
public function upload_Files($filefeild)
    {
        //set preferences
        $config['upload_path'] = 'assets/bcc/'.$filefeild."/";
        $config['allowed_types'] = 'doc|docx|xml|pdf|image|excel|jpg|png|jpeg';
        $config['max_size']    = 0;
        $config['filname'] = $filenamee = $_FILES[$filefeild]['name'];
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($filefeild))
        {
            // case - failure
            $upload_error = array('error' => $this->upload->display_errors());
        }
        else
        {
            // case - success
            $upload_data = $this->upload->data();
            $data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
            $filname = $upload_data['file_name'];
            return $filenamee;
        }


    }

1 个答案:

答案 0 :(得分:0)

如果您看到codeigniter的函数存在here,您将找到以下代码,

// Remove white spaces in the name
if ($this->remove_spaces === TRUE)
{
    $this->file_name = preg_replace('/\s+/', '_', $this->file_name);
}

这意味着codeigniter会删除间隔并默认添加_,可以通过设置config变量来更改。比如$config['remove_spaces'] = FALSE;

其次,如果您希望您的文件应该使用下划线保存,并且您获得了保存文件的实际名称,那么在您的代码中您必须进行如下所述的轻微更改。 你当前的代码

$config['filname'] = $filenamee = $_FILES[$filefeild]['name'];

将其更改为

$config['filname'] = $_FILES[$filefeild]['name'];

并且在成功案例中从$this->upload->data()

获取文件名

注意:您还可以找到答案here