简体   繁体   中英

How to upload file to server via ftp

I need to upload csv file to live server using ftp function in php

My code

 $local_filename = $_SERVER['DOCUMENT_ROOT'].'/feeds/ftp/xyz.csv';
 $remote_filename = date('Y-m-d_').'data.csv';

 $ftp_server = 'sftp.myhost.com';
 $ftp_user_name = 'username';
 $ftp_user_pass = 'password';
 $conn_id = ftp_connect($ftp_server,22);
 // login with username and password
 $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
 if (ftp_put($conn_id, $remote_filename, $local_filename, FTP_ASCII)) {
unlink($local_filename);
$res = true;
 } else {
$res = false;
 }
 ftp_close($conn_id);

And I am getting this:

 Warning: ftp_login() expects parameter 1 to be resource, boolean given in C:\wamp\www\fstp\index.php on line 11

How can I resolve this error?

It looks like you are trying to connect to an SFTP server, use ftp_ssl_connect instead.

Also, your code is not very robust. You should check the return value of ftp_connect or ftp_ssl_connect before you continue.

ftp_connect() returns FALSE on error. This is why your error message says that a there is a boolean given in the first argument.

Check to make sure your connection info is valid. In the future, You should also make sure the connection succeeded before trying to login:

if($conn_id) {
    ...
}

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