2017-09-29 1 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

관련 문제