Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

php - Getting "FTP server reports 550 Could not get file size." when using FTP URL in fopen

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");

and

$contents = file_get_contents('ftp://ftpuser:123456789@localhost/file.conf');
echo $contents;

Neither does work. I also tried to send directly GET request to the URL which also doesn't work. How can I read the FTP file without downloading?

I checked the php warning which says:

PHP Warning: file_get_contents(ftp://...@localhost/file.conf): failed to open stream: FTP server reports 550 Could not get file size.
in /var/www/html/api/listfolder.php on line 2

I'm sure that the file exists

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...