PHP 8.5.0 Alpha 1 available for testing

Voting

: max(two, three)?
(Example: nine)

The Note You're Voting On

anomie at users dot sf dot net
18 years ago
Why there isn't an "ftp_get_contents" function, I don't know. It takes a little work to emulate one, but it's doable.
<?php
function ftp_get_contents($ftp_stream, $remote_file, $mode, $resume_pos=null){
$pipes=stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
if(
$pipes===false) return false;
if(!
stream_set_blocking($pipes[1], 0)){
fclose($pipes[0]); fclose($pipes[1]);
return
false;
}
$fail=false;
$data='';
if(
is_null($resume_pos)){
$ret=ftp_nb_fget($ftp_stream, $pipes[0], $remote_file, $mode);
} else {
$ret=ftp_nb_fget($ftp_stream, $pipes[0], $remote_file, $mode, $resume_pos);
}
while(
$ret==FTP_MOREDATA){
while(!
$fail && !feof($pipes[1])){
$r=fread($pipes[1], 8192);
if(
$r==='') break;
if(
$r===false){ $fail=true; break; }
$data.=$r;
}
$ret=ftp_nb_continue($ftp_stream);
}
while(!
$fail && !feof($pipes[1])){
$r=fread($pipes[1], 8192);
if(
$r==='') break;
if(
$r===false){ $fail=true; break; }
$data.=$r;
}
fclose($pipes[0]); fclose($pipes[1]);
if(
$fail || $ret!=FTP_FINISHED) return false;
return
$data;
}
?>

Something similar would work to write a ftp_put_contents function, too.

<< Back to user notes page

To Top