Getting “FTP server reports 550 Could not get file size.” when using FTP URL in fopen

前端 未结 1 702
猫巷女王i
猫巷女王i 2020-12-11 13:54

I want to read a file which is on a remote ftp server to a variable. I tried reading with address

fopen(\"ftp://user:pass@localhost/filetoread\");

相关标签:
1条回答
  • 2020-12-11 14:11

    The PHP FTP URL wrapper seems to require FTP SIZE command, what your FTP server does not support.

    Use the ftp_fget instead:

    $conn_id = ftp_connect('hostname');
    
    ftp_login($conn_id, 'username', 'password');
    ftp_pasv($conn_id, true);
    
    $h = fopen('php://temp', 'r+');
    
    ftp_fget($conn_id, $h, '/path/to/file', FTP_BINARY, 0);
    
    $fstats = fstat($h);
    fseek($h, 0);
    $contents = fread($h, $fstats['size']); 
    
    fclose($h);
    ftp_close($conn_id);
    

    (add error handling)

    See PHP: How do I read a .txt file from FTP server into a variable?

    0 讨论(0)
提交回复
热议问题