You are viewing revision #3 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
First of all this is a very common problem, for more details see this post http://www.yiiframework.com/forum/index.php/topic/52052-upload-file-with-any-charset/
I tried to solve this but there is no easy way (or it is not possible).
So the only way that I found is knowing the original character encoding
Suppose the encoding is the Greek, so in this case:
In your model that is related with this file (suppose the table of this model has the id and name_file columns, etc)
public $file;
public function rules() {
return array(
...
array('file', 'file', 'types' => 'doc,pdf,docx,txt,xlsx,doc,xls,png,jpg,gif,rtf'),
...
);
}
public beforeSave() {
if (parent::beforeSave()) {
$model->file = CUploadedFile::getInstance($model, 'file');
if ($model->file) {
$name_file = $model->file->name;
$model->name_file = $name_file;
$name_file = iconv('UTF-8', 'greek//TRANSLIT//IGNORE', $model->name_file);
$model->file->saveAs('images/' . $name_file );
return true;
}
return true;
}
return false;
}
public function getFile() {
$encodedFile = iconv('UTF-8', 'greek//TRANSLIT//IGNORE', $this->name_file);
$path = 'images/' . $encodedFile;
if (file_exists($path))
Yii::app()->getRequest()->sendFile($model->name_file, file_get_contents($path));
else
throw new CHttpException(404,'The requested file does not exists.'); //or whatever you want because this function is a model method
}
public function beforeDelete() {
$path = 'images/' . $this->name_file;
$path = iconv('UTF-8', 'greek//TRANSLIT//IGNORE', $path);
@unlink($path);
return parent::beforeDelete();
}
In your controller
public function actionCreate()
{
$model=new YourModel();
if(isset($_POST['YourModel']))
{
$model->attributes=$_POST['YourModel'];
$model->save();
}
$this->render('create', array('model'=>$model));
}
public function actionView($id) {
$this->loadModel($id)->getFile();
}
The left code is similar to this wiki http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model
Notes:
If you want to save filenames in another encoding you have to change the greek to the corresponding encoding.
If you want to save filenames in variety encoding (for each file) you have to save the specific encoding into the record (in seperate column) and call the iconv on the fly with the appropriate parameter.
warning about winscp
using winscp with scp protocol you can be cheated. SCP does not support utf 8 names on remote server. Use sftp instead. It can be misunderstanding on debug your project on file names.
RE: warning about winscp
Hi Alexufo
I had searched for other (except http(s)) protocols. And I understood that they have fewer issues about the encoding names of the uploaded files.
In our case we have to use http protocol using a web application, so the solution that I mentioned is for http and https protocol
If I didn't understand something well please inform me :)
RE: RE: warning about winscp
hi KonApaz, i mean UTF-8 is not supported with SCP protocol (most convenient) in winscp. Some people who use winscp like me can mis understand about real filename at remote server.
I tryed get cyrillic filenames many many times until got facepalm about scp protocol)).
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.