代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
$file = "test.dat";

$ftp_server="ftp.server.com";
$ftp_user = "myname";
$ftp_pass = "mypass";
$destination_file = "test.dat";


$cid=ftp_connect($ftp_server);
if(!$cid) {
exit("Could not connect to server: $ftp_server\n");
}

$login_result = ftp_login($cid, $ftp_user, $ftp_pass);
if (!$login_result) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user";
}

$upload = ftp_put($cid, $destination_file, $file, FTP_BINARY);
if (!$upload) {
echo "Failed upload for $source_file to $ftp_server as $destination_file<br>";
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

ftp_close($cid);
?>

原因是没有定义ftp的主被动模式,true是被动模式:

1
ftp_pasv($cid, true);