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

Hi Monks!

I have a fileupload form submitting a file to this Perl script that I need to FTP once I have the file.
I am getting and error, "No such file or directory".
I am not passing the value correct from the fileupload into the FTP properly, I think.
Any suggestion on how to get this right?
The code sample:
#!/usr/bin/perl use strict ; use warnings ; use CGI ; use Net::FTP; my $cgi = CGI->new(); my $file = $cgi->param( 'upload' ) || ''; do_ftp($file); sub do_ftp { my $filename = @_; return unless $filename ; $cgi->upload( 'upload' ); my $tmp = $cgi->tmpFileName( $filename ) ; rename( $tmp, $filename ) ; chmod 0664, $filename ; my $host = 'xxx'; my $user = 'user'; my $pass = 'pass'; my $ftpdir = '/'; my $ftp = Net::FTP->new($host, Debug => 0) or die "Could not connect to '$host': $@"; $ftp->login($user, $pass) or die sprintf "Could not login: %s", $ftp->message; $ftp->cwd($ftp_dir) or die sprintf "Could not login: %s", $ftp->message; # FTP file. $ftp->put($filename) or die "Cannot put file ", $ftp->message; $ftp->quit; }

Thanks for Helping!

Replies are listed 'Best First'.
Re: Error ftping file with Net::FTP
by thanos1983 (Parson) on Aug 16, 2017 at 14:55 UTC

    Hello Anonymous Monk,

    Try something like this:

    #!/usr/bin/perl use CGI; use strict; use warnings; use Net::FTP; use feature 'say'; my $cgi = CGI->new(); my $file = $cgi->param( 'upload' ) || 'test.pl'; say do_ftp($file); sub do_ftp { my ($filename) = @_; return unless $filename ; } __END__ $ perl ftp.pl test.pl

    We do not have the full code but I assume my $file = $cgi->param( 'upload' ) || ''; that you are passing an empty string here right? I have alter the code in two points. Observe the () in the subroutine also.

    I would recommend to use module Net::SFTP::Foreign. Similar question with samples of working code(s) Generate temporary file and FTP upload.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      I was missing the "()" in the sub, thanks for pointing that out!
      Is there a bug in Net::FTP?
      I am checking if the file exists using this command:
      my @check = $ftp->ls("$filename"); I am getting an error:
      Can't use an undefined value as a symbol reference at /Net/FTP/datacon +n.pm line 54.

      Any suggestions?
Re: Error ftping file with Net::FTP
by dbander (Scribe) on Aug 16, 2017 at 15:00 UTC

    I don't see where you are extracting the base filename out of the fully-qualified filename likely being sent by the form. I seem to recall that's an important step.

    /usr/foo/bar/file.txt would need to become file.txt for example.

      I don't see where you are extracting the base filename out of the fully-qualified filename likely being sent by the form.

      Well, any SANE browser just sends the base filename and extension of the local file. MSIE, on the other hand, DOES send a fully qualified name, for no reason. So it looks like code never tested with a broken browser.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        firefox also sends the full filename