PHP ftp_put失败

6fe3ivhb  于 2022-12-17  发布在  PHP
关注(0)|答案(2)|浏览(186)

我通过FTP上传XML文件:

$ftp = "ftp";
$username = "username";
$pwd = "password";
$filename = $_FILES[$xyz][$abc];
$tmp = $_FILES['file']['tmp_name'];
$destination = "/Content/EnquiryXML ";

$connect = ftp_connect($ftp)or die("Unable to connect to host");
ftp_login($connect,$username,$pwd)or die("Authorization Failed");
echo "Connected!<br/>";              

if(!$filename)
{
    echo"Please select a file";
}

else
{
    ftp_put($connect,$destination.'/'.$filename,$tmp,FTP_ASCII)or die("Unable to upload");
    echo"File successfully uploaded to FTP";
}

我想将使用DOMDocument创建的XML文件发送到FTP服务器,但无法执行。
ftp_put返回false。

unguejic

unguejic1#

ftp_put(或其他任何传输命令,如ftp_getftp_nlistftp_rawlistftp_mlsd)出现问题的最典型原因是PHP默认为主动模式,在99%的情况下,必须切换到被动模式才能使传输工作。

$connect = ftp_connect($ftp) or die("Unable to connect to host");
ftp_login($connect, $username, $pwd) or die("Authorization failed");
// turn passive mode on
ftp_pasv($connect, true) or die("Unable switch to passive mode");
  • 必须在ftp_login之后调用ftp_pasv。之前调用它无效。*

另见:

此外,如果您的FTP服务器在PASV命令的响应中报告了不正确的IP地址(如果服务器位于防火墙/NAT之后,这是很常见的),您可能需要使用以下命令来解决此问题:

ftp_set_option($connect, FTP_USEPASVADDRESS, false);

参见PHP FTP + Passive FTP Server Behind NAT

  • 尽管在这种情况下正确的解决方案是修复服务器。*
bnlyeluc

bnlyeluc2#

这起了作用:

// connect and login to FTP server
$ftp_server = "host";
$ftp_username = "username";
$ftp_userpass = "password";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);
$file ="$abc";

// upload file 
if (ftp_put($ftp_conn, "/$abc" , $file, FTP_ASCII)){
    echo "Successfully uploaded $file.";
} else {
    echo "Error uploading $file";
}

// close connection
ftp_close($ftp_conn);

相关问题