2017-09-29 53 views
0

リモートNASサーバーからファイルをダウンロードしたいと思いますが、クライアントにダウンロードすることはできません。私はこの機能を使用しています:CakePHP 3でリモートファイルをダウンロードする方法は?

public function download(){ 
    // Set IP and port 
    define("FTP_CONNECT_IP", "xxx"); 
    define("CONNECT_PORT", "21"); 

    // Set username and password 
    define("FTP_LOGIN_USER", "username"); 
    define("FTP_LOGIN_PASS", "password"); 
    $remote_file = 'ftp://' . FTP_LOGIN_USER . ':' . FTP_LOGIN_PASS . '@' . FTP_CONNECT_IP . '/' .'PathToFile.avi'; 
    $response = $this->response->withFile($remote_file,['download' => true]); 
    return $response; 

    } 

ブラウザは決して読んでいませんが、ブラウザはダウンロードを求めません。何が間違っていますか?

答えて

1

リモートファイルにResponse::withFile()を使用することはできません。ローカルファイルでのみ動作します。

リモートファイルを提供する場合は、一時的にサーバーに保存するか、レスポンス本文のCakePHPコールバックストリームなどを使用して手動でデータを出力する必要があります。

return $this->response 
    ->withType(pathinfo($remote_file, \PATHINFO_EXTENSION)) 
    ->withDownload(basename($remote_file)) 
    ->withLength(filesize($remote_file)) 
    ->withBody(new \Cake\Http\CallbackStream(function() use ($remote_file) { 
     ob_end_flush(); 
     ob_implicit_flush(); 
     readfile($remote_file); 
    })); 

が、これは私をたくさん助け、また

+0

感謝を参照してください:

は、ここで簡単な例(範囲要求をサポートしていません)です。 – Stefan

関連する問題