Yii 1.1: Save uploaded file with any filename characters


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));
              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);
        return parent::beforeDelete();

In your controller

public function actionCreate()
        $model=new YourModel();
        $this->render('create', array('model'=>$model));
 public function actionView($id) {

The left code is similar to this wiki http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model


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.

Total 3 comments

#17336 report it
Alexufo at 2014/05/25 12:33pm
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)).

#17335 report it
Kostas Apazidis (KonApaz) at 2014/05/25 09:43am
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 :)

#17334 report it
Alexufo at 2014/05/25 07:13am
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.

Leave a comment

Please to leave your comment.

Write new article