62 if (!defined(
"FTP_BINARY")) {
63 define(
"FTP_BINARY", 1);
65 if (!defined(
"FTP_ASCII")) {
66 define(
"FTP_ASCII", 0);
75 $this->
ftp_debug(
"Trying to ".$server.
":".$port.
" ...\n");
76 $this->ftp_sock = @fsockopen($server, $port, $errno, $errstr, $this->timeout);
78 if (!$this->ftp_sock || !$this->
ftp_ok()) {
79 $this->
ftp_debug(
"Error : Cannot connect to remote host \"".$server.
":".$port.
"\"\n");
80 $this->
ftp_debug(
"Error : fsockopen() ".$errstr.
" (".$errno.
")\n");
84 if(substr($this->ftp_resp, 0, 3) !==
'220')
89 $this->
ftp_debug(
"Connected to remote host \"".$server.
":".$port.
"\"\n");
98 $this->
ftp_debug(
"Error : USER command failed\n");
104 $this->
ftp_debug(
"Error : PASS command failed\n");
107 $this->
ftp_debug(
"Authentication succeeded\n");
116 $this->
ftp_debug(
"Error : PWD command failed\n");
120 return preg_replace(
"@^[0-9]{3} \"(.+)\" .+\r\n@",
"\\1", $this->ftp_resp);
127 $this->
ftp_debug(
"Error : SIZE command failed\n");
131 return preg_replace(
"@^[0-9]{3} ([0-9]+)\r\n@",
"\\1", $this->ftp_resp);
138 $this->
ftp_debug(
"Error : MDTM command failed\n");
141 $mdtm = preg_replace(
"@^[0-9]{3} ([0-9]+)\r\n@",
"\\1", $this->ftp_resp);
142 $date = sscanf($mdtm,
"%4d%2d%2d%2d%2d%2d");
143 $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]);
152 $this->
ftp_debug(
"Error : SYST command failed\n");
155 $DATA = explode(
" ", $this->ftp_resp);
163 $response = $this->
ftp_ok();
165 $this->
ftp_debug(
"Error : CDUP command failed\n");
173 $response = $this->
ftp_ok();
175 $this->
ftp_debug(
"Error : CWD command failed\n");
183 $response = $this->
ftp_ok();
185 $this->
ftp_debug(
"Error : DELE command failed\n");
193 $response = $this->
ftp_ok();
195 $this->
ftp_debug(
"Error : RMD command failed\n");
203 $response = $this->
ftp_ok();
205 $this->
ftp_debug(
"Error : MKD command failed\n");
212 if (!($remote_list = $this->
ftp_nlist(
"-a"))) {
213 $this->
ftp_debug(
"Error : Cannot get remote file list\n");
218 while (list(,$value) = each($remote_list)) {
219 if ($value == $pathname) {
220 $this->
ftp_debug(
"Remote file ".$pathname.
" exists\n");
224 $this->
ftp_debug(
"Remote file ".$pathname.
" does not exist\n");
233 $this->
ftp_debug(
"Error : RNFR command failed\n");
238 $response = $this->
ftp_ok();
240 $this->
ftp_debug(
"Error : RNTO command failed\n");
247 if (!($string = $this->
ftp_pasv())) {
254 $nlst =
"NLST ".$arg;
259 if (!$sock_data || !$this->
ftp_ok()) {
260 $this->
ftp_debug(
"Error : Cannot connect to remote host\n");
261 $this->
ftp_debug(
"Error : NLST command failed\n");
264 $this->
ftp_debug(
"Connected to remote host\n");
267 while (!feof($sock_data)) {
268 $list[] = preg_replace(
"@[\r\n]@",
"", fgets($sock_data, 512));
274 $this->
ftp_debug(
"Error : NLST command failed\n");
283 if (!($string = $this->
ftp_pasv())) {
290 if (!$sock_data || !$this->
ftp_ok()) {
291 $this->
ftp_debug(
"Error : Cannot connect to remote host\n");
292 $this->
ftp_debug(
"Error : LIST command failed\n");
296 $this->
ftp_debug(
"Connected to remote host\n");
298 while (!feof($sock_data)) {
299 $list[] = preg_replace(
"@[\r\n]@",
"", fgets($sock_data, 512));
305 $this->
ftp_debug(
"Error : LIST command failed\n");
312 function ftp_get($localfile, $remotefile, $mode = 1)
316 if (@file_exists($localfile)) {
317 $this->
ftp_debug(
"Warning : local file will be overwritten\n");
320 $fp = @fopen($localfile,
"w");
322 $this->
ftp_debug(
"Error : Cannot create \"".$localfile.
"\"");
323 $this->
ftp_debug(
"Error : GET command failed\n");
328 $this->
ftp_debug(
"Error : GET command failed\n");
332 if (!($string = $this->
ftp_pasv())) {
333 $this->
ftp_debug(
"Error : GET command failed\n");
340 if (!$sock_data || !$this->
ftp_ok()) {
341 $this->
ftp_debug(
"Error : Cannot connect to remote host\n");
342 $this->
ftp_debug(
"Error : GET command failed\n");
345 $this->
ftp_debug(
"Connected to remote host\n");
346 $this->
ftp_debug(
"Retrieving remote file \"".$remotefile.
"\" to local file \"".$localfile.
"\"\n");
347 while (!feof($sock_data)) {
348 fputs($fp, fread($sock_data, 4096));
354 $response = $this->
ftp_ok();
356 $this->
ftp_debug(
"Error : GET command failed\n");
361 function ftp_put($remotefile, $localfile, $mode = 1)
364 if (!@file_exists($localfile)) {
365 $this->
ftp_debug(
"Error : No such file or directory \"".$localfile.
"\"\n");
366 $this->
ftp_debug(
"Error : PUT command failed\n");
370 $fp = @fopen($localfile,
"r");
372 $this->
ftp_debug(
"Error : Cannot read file \"".$localfile.
"\"\n");
373 $this->
ftp_debug(
"Error : PUT command failed\n");
378 $this->
ftp_debug(
"Error : PUT command failed\n");
382 if (!($string = $this->
ftp_pasv())) {
383 $this->
ftp_debug(
"Error : PUT command failed\n");
390 if (!$sock_data || !$this->
ftp_ok()) {
391 $this->
ftp_debug(
"Error : Cannot connect to remote host\n");
392 $this->
ftp_debug(
"Error : PUT command failed\n");
395 $this->
ftp_debug(
"Connected to remote host\n");
396 $this->
ftp_debug(
"Storing local file \"".$localfile.
"\" to remote file \"".$remotefile.
"\"\n");
398 fputs($sock_data, fread($fp, 4096));
404 $response = $this->
ftp_ok();
406 $this->
ftp_debug(
"Error : PUT command failed\n");
414 $response = $this->
ftp_ok();
416 $this->
ftp_debug(
"Error : SITE command failed\n");
424 if (!$this->
ftp_ok() || !fclose($this->ftp_sock)) {
425 $this->
ftp_debug(
"Error : QUIT command failed\n");
428 $this->
ftp_debug(
"Disconnected from remote host\n");
442 $response = $this->
ftp_ok();
444 $this->
ftp_debug(
"Error : TYPE command failed\n");
452 $response = $this->
ftp_ok();
454 $this->
ftp_debug(
"Error : PORT command failed\n");
463 $this->
ftp_debug(
"Error : PASV command failed\n");
467 $ip_port = preg_replace(
"@^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*\r\n$@",
"\\1", $this->ftp_resp);
474 $cmd = $cmd.
" ".$arg;
477 fputs($this->ftp_sock, $cmd.
"\r\n");
485 $this->ftp_resp =
"";
489 while(($char = fgetc($this->ftp_sock)) !== FALSE)
492 if($char ===
"\n")
break;
496 if(!preg_match(
'@^[0-9]{3}@', $line))
501 $this->ftp_resp = $line;
506 $code = substr($line, 0, 3);
512 while(($char = fgetc($this->ftp_sock)) !== FALSE)
515 if($char ===
"\n")
break;
517 $this->ftp_resp .= $line;
520 if($code .
' ' === substr($line, 0, 4))
break;
524 $this->
ftp_debug(str_replace(
"\r\n",
"\n", $this->ftp_resp));
526 if (!preg_match(
"@^[123]@", $this->ftp_resp)) {
535 $this->
ftp_debug(
"Disconnected from remote host\n");
536 return fclose($sock);
541 if (!preg_match(
"@[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+@", $ip_port)) {
542 $this->
ftp_debug(
"Error : Illegal ip-port format(".$ip_port.
")\n");
546 $DATA = explode(
",", $ip_port);
547 $ipaddr = $DATA[0].
".".$DATA[1].
".".$DATA[2].
".".$DATA[3];
548 $port = $DATA[4]*256 + $DATA[5];
549 $this->
ftp_debug(
"Trying to ".$ipaddr.
":".$port.
" ...\n");
550 $data_connection = @fsockopen($ipaddr, $port, $errno, $errstr);
551 if (!$data_connection) {
552 $this->
ftp_debug(
"Error : Cannot open data connection to ".$ipaddr.
":".$port.
"\n");
553 $this->
ftp_debug(
"Error : ".$errstr.
" (".$errno.
")\n");
557 return $data_connection;
ftp_file_exists($pathname)
ftp_putcmd($cmd, $arg="")
ftp_connect($server, $port=21)
ftp_get($localfile, $remotefile, $mode=1)
ftp_put($remotefile, $localfile, $mode=1)
ftp_nlist($arg="", $pathname="")
ftp_open_data_connection($ip_port)
ftp_rawlist($pathname="")
ftp_close_data_connection($sock)