工作解決方案(使用PORT完整重寫以前的解決方案)。正確的順序是
PASV
Server responds with something like
"227 Entering Passive Mode (127,0,0,1,30,235)"
STOR /path/on/remote/server/foo.txt
=> Now we have to connect to socket 30*256+235 on 127.0.0.1 and send the data.
Done
代碼
$fp = ftp_connect("127.0.0.1", 21, 10) or die("foo");
ftp_login ($fp, "anonymous", "password");
ftp_raw_send_file($fp, "/local/path/to/file.txt", "foo/foo.txt");
function ftp_raw_send_file($fp, $localfile, $remotefile) {
$connect = ftp_raw($fp, "PASV");
// parse the response and build the IP and port from the values
if (count($connect) > 0 && preg_match("/.*\((\d+),(\d+),(\d+),(\d+),(\d+),(\d+)\)/", $connect[0], $m)) {
$address="{$m[1]}.{$m[2]}.{$m[3]}.{$m[4]}";
$port=$m[5] * 256 + $m[6];
print_r(ftp_raw($fp, "STOR $remotefile"));
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
if ($sock) {
socket_connect($sock, $address, $port);
socket_write($sock, file_get_contents($localfile));
socket_close($sock);
}
}
}
您是否花時間結帳[RFC959 - 文件傳輸協議(FTP)](http://tools.ietf.org/html/rfc959)?它寫得很好,非常值得一讀。 – 2012-03-11 19:28:15