我上传文件时文件名会自动更改

时间:2015-06-19 05:21:59

标签: file-upload codeigniter-2

我是CodeIgniter的新手,遇到以下问题。当我上传文件时,它成功上传到我的本地文件夹并将文件名保存到数据库中。问题是,上传后文件名已更改。 例如: 文件名“screenshot image 123.png” - > “screenshot_image_123.png” 它只是将空间转换为下划线;只有当文件名具有空格时才会出现此问题。除此之外,所有其他文件都成功上传。下面是我在控制器页面上使用的代码:

public function upload_pic()
{
               $image_path = realpath(APPPATH . '../uploads');
               $config['upload_path'] = $image_path;
               $config['allowed_types'] =   "gif|jpg|jpeg|png";
               $config['file_name'] = $_FILES['file']['name']; 
               $config['encrypt_name'] = TRUE; 
               $config['overwrite'] = TRUE;
               $this->load->library('upload',$config);
               $this->upload->initialize($config);  
               if(!$this->upload->do_upload('file'))
               {
                        echo $this->upload->display_errors();
               }
               else
               {
                        $finfo=$this->upload->data();
                        $data['uploadInfo'] = $finfo;
               }
}

任何人都可以帮助我????

1 个答案:

答案 0 :(得分:0)

在保存文件名之前尝试生成一些独特的

$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);

$filename = sha1_file($filename). md5(date('Y-m-d H:i:s:u')) . '.'. $ext; //not only this you can generate any format



$config['file_name'] = $filename; //Use this file name is db too