Home  >  Q&A  >  body text

PHP's ftp_put operation failed

<p>I upload the XML file via FTP: </p> <pre class="brush:php;toolbar:false;">$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"; }</pre> <p>I want to send an XML file created using <code>DOMDocument</code> to an FTP server, but I cannot succeed. </p> <p><code>ftp_put</code> returns false. </p>
P粉884548619P粉884548619444 days ago697

reply all(2)I'll reply

  • P粉225961749

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

    reply
    0
  • P粉056618053

    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_pasvfunction.

    $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.

    reply
    0
  • Cancelreply