카테고리 없음

[PHP] 대용량 파일과 함께 ssh2_scp_recv를 사용하는 방법 (메모리 제한)

행복을전해요 2021. 2. 21. 10:36

이것은 작동하고 매우 빠릅니다

https://github.com/phpseclib/phpseclib

암호

require_once 'Net/SFTP.php';

$sftp = new Net_SFTP($host);
    if (!$sftp->login('root', 'password') {
        exit('Login Failed');
        }
        
        $sftp->get('remote_file.ext', 'local_file.ext');
        
-------------------

대신에 사용하는 ssh2_scp_recv(), 당신은 사용해 볼 수 있습니다 ssh2.sftp://와 래퍼 fopen(). 이렇게하면 파일을 청크로 읽을 수 있으므로 메모리를 너무 많이 사용하지 않습니다.

$connection = ssh2_connect('your.server.tld', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);

$remote = fopen("ssh2.sftp://$sftp/path/to/file", 'rb');
$local = fopen('/path/to/file', 'w');

while(!feof($remote)){
    fwrite($local, fread($remote, 8192));
    }
    
    fclose($local);
    fclose($remote);
    

면책 조항 : 나는 이것을 테스트하지 않았지만 작동해야 할 것 같습니다.

출처 :

  • ssh2_sftp: http://www.php.net/manual/en/function.ssh2-sftp.php
  • fwrite훔친 루프 : http://www.php.net/stream_copy_to_stream#98119


출처
https://stackoverflow.com/questions/22079966