P粉2259617492023-08-25 18:30:59
This worked:
// 连接并登录到FTP服务器 $ftp_server = "主机"; $ftp_username = "用户名"; $ftp_userpass = "密码"; $ftp_conn = ftp_connect($ftp_server) or die("无法连接到 $ftp_server"); $login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass); $file ="$abc"; // 上传文件 if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){ echo "成功上传文件 $file。"; } else { echo "上传文件 $file 出错"; } // 关闭连接 ftp_close($ftp_conn);
P粉0566180532023-08-25 15:46:37
results in ftp_put
(or any other transfer command such as ftp_get
, ftp_nlist
, ftp_rawlist
, ftp_mlsd
) The most common reason for problems is that PHP defaults to active mode. In 99% of cases, switching to passive mode is required for the transmission to work properly. Use ftp_pasv
function.
$connect = ftp_connect($ftp) or die("无法连接到主机"); ftp_login($connect, $username, $pwd) or die("授权失败"); // 打开被动模式 ftp_pasv($connect, true) or die("无法切换到被动模式");
ftp_pasv
must be called after ftp_login
. Calling it before has no effect.
See also:
Additionally, if your FTP server reports the wrong IP address when responding to the PASV
command (this is fairly common if the server is behind a firewall/NAT), you may need to do this by using solve:
ftp_set_option($connect, FTP_USEPASVADDRESS, false);
SeePHP FTP Passive FTP server is behind NAT.
Although in this case the correct solution is to repair the server.