Bloehdian has asked for the wisdom of the Perl Monks concerning the following question:

The following code is intended to simply transfer a file stored on hard disk to a remote sftp server:

sub sftp_transfer { my ( $ftp_url, $ftp_user, $ftp_passwd, $ftp_dir, $output_file_path +) = @_; if ( $debug ) { print "&sftp_transfer: "; print "Host: $ftp_url\n"; print "User: $ftp_user\n"; print "Password: $ftp_passwd\n"; print "Dir: $ftp_dir\n"; } my %conn = ( "user" => $ftp_user, "password" => $ftp_passwd, "port" => 2222, "autoflush" => 1 ); my $ftp=Net::SFTP::Foreign->new( $ftp_url, %conn ); if ( $ftp->error ) { $err_occured = 1; $error_msg ="SFTP: Cannot connect to sftp-server $ftp_url: " . $ftp->err +or; die ( "\n" ) }; $ftp->setcwd( $ftp_dir ) or do { $err_occured = 1; $error_msg = "SFTP: Cannot cd into $ftp_dir: " . $ftp->error; die ( "\n" ) }; my $output_file_name = basename( $output_file_path ); $ftp->put( $output_file_path, $output_file_name ) or do { $err_occured = 1; $error_msg = "SFTP: Cannot upload file $output_file_path: " . $f +tp->error; die ( "\n" ) }; }

Output (with $debug == 1)

&sftp_transfer: Host: example.com User: user Password: *** Dir: /POS/21/2100139 Password Authentication

On the command line, file transfer works flawlessly. When applying the above code, the files transferred have zero Byte size, although not being empty prior to transfer, The sending client is a Linux box, I not know about the server. Any ideas whatis going wrong here?

Replies are listed 'Best First'.
Re: Net::Sftp::Foreign writes empty files to FTP server
by salva (Canon) on Feb 08, 2020 at 10:11 UTC
    Net::SFTP::Foreign has a debug mode you can enable adding the following line at the beginning of the script:
    $Net::SFTP::Foreign::debug = -1;
    Enable it and then post here the output (though, you should take care to remove passwords or any other sensible information from the output)
Re: Net::Sftp::Foreign writes empty files to FTP server
by soonix (Chancellor) on Feb 07, 2020 at 16:39 UTC
    Is the file readable for your script? (see -X)
    Does your input path point to the correct location and why do you name your input path $output_file_path?
Re: Net::Sftp::Foreign writes empty files to FTP server
by Naren (Initiate) on Jan 06, 2022 at 18:13 UTC
    It must be due to put method trying to copy the time and permission of the file. Try passing copy_time => 0, copy_perm => 0 params to Put method.