Cakephp response cannot read UTF-8 file name

I want to upload a file after checking the input, so I wrote a function in my controller, for example

// Function to check login and download News PDF file
public function download(){

    if($this->Auth->user()){ 
        // Get the news file path from newsId 
        $pNewsObj  = ClassRegistry::init('PublicNews');
        $news = $pNewsObj->findById($newsId);

        $filePath = ROOT.DS.APP_DIR.DS.'webroot/upload_news'.DS.$news['PublicNews']['reference'];
        // Check if file exists
        if(!file_exists($filePath)){
            return $this->redirect('/404/index.php');
        }
        $this->response->charset('UTF-8');
        //$this->response->type('pdf');
        $this->response->file('webroot/upload_news'.DS.$news['PublicNews']['reference'],  array('download' => true, 'name' => $news['PublicNews']['reference']));
        //$this->response->download($news['PublicNews']['reference']);
        return $this->response;
    }else{
        return $this->redirect(array('controller'=> 'users', 'action' => 'login'));
    }
} 

Now everything works fine if required.

PROBLEM: when the file name is in UTF-8, for example. テ ス ト .pdf (its Test.pdf is in Japanese) cakephp causes such an error.

enter image description here

It works fine for an English file name, but my client wants the file name to be the same as the downloaded one, so I cannot change the file name to English.

+4
source share
3 answers

, mb_detect_encoding(), . , SJIS. SJIS, Windows SJIS .

. File, , SJIS, Response::file(). .

public function download(){

    if($this->Auth->user()){ 
        // Get the news file path from newsId 
        $pNewsObj  = ClassRegistry::init('PublicNews');
        $news = $pNewsObj->findById($newsId);

        if (!$news) {
            throw new NotFoundException();
        }

        $fileName = mb_convert_encoding($news['PublicNews']['reference'], 'SJIS-win', 'UTF8');

        // Directory traversal protection
        if (strpos($fileName, '..') !== false) {
            throw new ForbiddenException();
        }

        $filePath = WWW_ROOT . 'upload_news' . DS . $fileName;
        if (!is_readable($filePath)) {
            throw new NotFoundException();
        }

        if (function_exists('mime_content_type')) {
            $type = mime_content_type($filePath);
            $this->response->type( $type );
        } else {
            // TODO: If Finfo extension is not loaded, you need to detect content type here;
        }

        $this->response->download( $fileName );
        $this->response->body( file_get_contents($filePath) );

        return $this->response;
    }else{
        return $this->redirect(array('controller'=> 'users', 'action' => 'login'));
    }
} 

SJIS UTF8, . SJIS . SJIS ascii . (\). , 表 (955C) (5C = ). , .表 .十 , - 10.能 , .

UTF-8, SJIS, . explode() SJIS. strpos() . FTP SCP? , SJIS UTF-8 UTF-8 SJIS .

+2

, , .

    public function change_file_name($fileName= '') {
        $ext            =   pathinfo($fileName, PATHINFO_EXTENSION);
        $fileName       =   'file_'.time().".".$ext;
        $exFileName     =   strtolower(substr($fileName,strrpos($fileName,".") + 1));
        $sampleFileName =   str_replace('.'.$exFileName,'', $fileName);
        $name           =   Sanitize::paranoid($sampleFileName,array('_'));
        $fileRename     =   $name.'.'.$exFileName;
        return $fileRename;
    }

    $return_file_name   =     $this->change_file_name($file_name);
    if($this->moveUploadedFile($tmp_name,WEBSITE_PROFILE_ROOT_PATH.$return_file_name)){
        $saveData['profile_image']          =   $return_file_name;
    }

, . ​​,

0

, Tom Scott , base64 Unicode PHP.

, , base64 . , ASCII, .

/ % 2F, .

, ,

0

All Articles