PHP ftp_put fails

徘徊边缘 提交于 2020-08-24 04:14:42

问题


I upload XML file through FTP:

$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";
}

I want to send the XML file created using DOMDocument to a FTP server but I am not able.

The ftp_put returns false.


回答1:


Most typical cause of problems with ftp_put (or any other transfer command like ftp_get, ftp_nlist, ftp_rawlist, ftp_mlsd) is that PHP defaults to the active mode. And in 99% cases, one has to switch to the passive mode, to make the transfer working. Use the ftp_pasv function.

$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");

See also:

  • PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"
  • my article on the active and passive FTP connection modes.



回答2:


This worked:

// 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); 


来源:https://stackoverflow.com/questions/62989425/how-to-fix-error-425-cant-open-data-connection-for-transfer-of-php-ftp-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!