使用cakephp的插件Fileupload上传中文文件名失败,跟踪代码,发现,得到的文件名不对,如果这个文件是第一次上传,得不到名字,如果是第二次上传,会得到-1.png名字,中文得不到.
解决办法:
在plugins/Fileupload/vendors/uploader.php文件中第#140行,将
$target_path = $up_dir . DS . $fileName; $target_path = $this->__handleUnique($target_path); //now move the file. if(move_uploaded_file($this->file['tmp_name'], $target_path)){ $this->finalFile = basename($target_path); return $this->finalFile; }
替换成
$path_parts = pathinfo($fileName); if(!array_key_exists('extension',$path_parts)){ $this->_error('Unable to get temp file extension.'); return false; } $target_path = $up_dir . DS . base64_encode($fileName).'.'.$path_parts['extension']; $target_path = $this->__handleUnique($target_path); //now move the file. if(move_uploaded_file($this->file['tmp_name'], $target_path)){ $this->finalFile = basename($target_path); return $this->finalFile; }
这样就可以得到上传的文件了,其实这块是将中文文件名进行了编码操作,不使用中文命名的方法.
在这里提示下,尽量不要上传中文文件名的文件,经常会出现一些问题.不过做网站的,控制不了这个用户行为,so 写完上传的功能模块后,主动多测试下.