dpthuyh1678
dpthuyh1678
2016-01-28 13:44
浏览 184
已采纳

php中的ftp_connect失败

I want to connect to a remote server whose ip adress is xx.xxx.xxx.xx to later write on a file on it. Here my php code:

<?php
$remote_file = 'test_remote.txt';
$file = 'test.txt';

$conn_id = ftp_connect('xx.xxx.xxx.xx') or die("Could not connect")
$login_result = ftp_login($conn_id, 'name', 'passwd');

if (ftp_fput($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "ok";
} else {
    echo "failed";
}

ftp_close($conn_id);
?>

I get the error "Could not connect"....

If I do

ssh name@xx.xxx.xxx.xx and entered the password it works fine. What is the problem?

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dptrmt4366
    dptrmt4366 2016-01-28 14:30
    已采纳

    You need to install ssh ftp extension, you can view the download documentation here

    When you have installed it and its working properly(remember to restart your webserver after installation), you can do the following:

    $connection = ssh2_connect('ip/address', 'port');
    if(ssh2_auth_password($connection, 'username', 'password') {
            $sftp = ssh2_sftp($connection);
            $fileStream = fopen("ssh2.sftp://" . $sftp . "pathToFile", 'w');
            //Write your data with fwrite($fileStream, $data)
    }
    
    点赞 评论

相关推荐