PHP ftp_put失败

问题描述 投票:4回答:2

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

php file-upload ftp
2个回答
16
投票

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
投票

这有效:

// 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); 
© www.soinside.com 2019 - 2024. All rights reserved.