简体   繁体   中英

ftp_connect in php fails

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM