PHP ftp_put失败

时间:2016-11-21 12:31:17

标签: php file-upload ftp

我通过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。

2 个答案:

答案 0 :(得分:14)

ftp_put(或任何其他传输命令,如ftp_getftp_nlistftp_rawlist)出现问题的最常见原因是PHP默认为活动模式。在99%的情况下,必须切换到被动模式,以使传输工作。使用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");

另见:

答案 1 :(得分:-1)

这有效:

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