* PemFTP - An Ftp implementation in pure PHP
* @copyright Alexey Dotsenko
* @author Alexey Dotsenko
* @link https://www.phpclasses.org/package/1743-PHP-FTP-client-in-pure-PHP.html
* @license LGPL https://opensource.org/licenses/lgpl-license.html
* Socket Based FTP implementation
* @copyright Alexey Dotsenko
* @author Alexey Dotsenko
* @link https://www.phpclasses.org/package/1743-PHP-FTP-client-in-pure-PHP.html
* @license LGPL https://opensource.org/licenses/lgpl-license.html
class ftp_sockets extends ftp_base {
function __construct($verb=FALSE, $le=FALSE) {
parent::__construct(true, $verb, $le);
// <!-- --------------------------------------------------------------------------------------- -->
// <!-- Private functions -->
// <!-- --------------------------------------------------------------------------------------- -->
function _settimeout($sock) {
if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
$this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
$this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
function _connect($host, $port) {
$this->SendMSG("Creating socket");
if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
$this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
if(!$this->_settimeout($sock)) return FALSE;
$this->SendMSG("Connecting to \"".$host.":".$port."\"");
if (!($res = @socket_connect($sock, $host, $port))) {
$this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
function _readmsg($fnction="_readmsg"){
$this->PushError($fnction,'Connect first');
$tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
$this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
$go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
$this->_code=(int)$regs[1];
function _exec($cmd, $fnction="_exec") {
$this->PushError($fnction,'Connect first');
if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
$status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
$this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
$this->_lastaction=time();
if(!$this->_readmsg($fnction)) return FALSE;
function _data_prepare($mode=FTP_ASCII) {
if(!$this->_settype($mode)) return FALSE;
$this->SendMSG("Creating data socket");
$this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->_ftp_data_sock < 0) {
$this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
if(!$this->_settimeout($this->_ftp_data_sock)) {
if(!$this->_exec("PASV", "pasv")) {
if(!$this->_checkCode()) {
$ip_port = explode(",", preg_replace("/^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*$/s", "\\1", $this->_message));
$this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
$this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
$this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
$this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
else $this->_ftp_temp_sock=$this->_ftp_data_sock;
if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
$this->PushError("_data_prepare","cannot get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
if(!@socket_bind($this->_ftp_data_sock,$addr)){
$this->PushError("_data_prepare","cannot bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
if(!@socket_listen($this->_ftp_data_sock)) {
$this->PushError("_data_prepare","cannot listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
$this->PushError("_data_prepare","cannot get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
if(!$this->_checkCode()) {
function _data_read($mode=FTP_ASCII, $fp=NULL) {
$NewLine=$this->_eol_code[$this->OS_local];
if(is_resource($fp)) $out=0;
$this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
$this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
if($this->_ftp_temp_sock===FALSE) {
$this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
function _data_write($mode=FTP_ASCII, $fp=NULL) {
$NewLine=$this->_eol_code[$this->OS_local];
if(is_resource($fp)) $out=0;
$this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
$this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
if($this->_ftp_temp_sock===FALSE) {
$this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
$block=fread($fp, $this->_ftp_buff_size);
if(!$this->_data_write_block($mode, $block)) return false;
} elseif(!$this->_data_write_block($mode, $fp)) return false;
function _data_write_block($mode, $block) {
if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
$this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
$block=substr($block, $t);
@socket_close($this->_ftp_temp_sock);
@socket_close($this->_ftp_data_sock);
$this->SendMSG("Disconnected data from remote host");
@socket_close($this->_ftp_control_sock);
$this->SendMSG("Socket closed");