in reply to Re: Using Perl FTP to write a string variable as a file
in thread Using Perl FTP to write a string variable as a file

Hello wise men,

may I ask please, if this topic was solved? I have the same issue her but may be under other circumstances.

My situation is as follows, I did in the past a save of a file first and then trasfered it via Net::FTP. Now I get the content of the file and a file name and have to do FTP without saving it first.

I did understand that the first hint was to open a file handle with the pointer to a scalar variable. I will test this but I also would appreciate to know, if some reader of this had tested it and was successfully.

thanks in advance, regards, Thomas

  • Comment on Re^2: Using Perl FTP to write a string variable as a file

Replies are listed 'Best First'.
Re^3: Using Perl FTP to write a string variable as a file
by toohoo (Beadle) on Dec 22, 2014 at 11:16 UTC

    Hello again ...

    I have tested it with a testscript. It does not work. The error message is:

    Cannot open Local file FH: No such file or directory at test-ftpscalar.pl line 54 put failed (Passive=1) Type set to I ; localfilename(FH): [d:/Dokumente2/Server2/cgi-bin/out//test-ftpscala +r.xml], remotefilename: [test-ftpscalar.xml] at test-ftpscalar.pl lin +e 54.

    The script is as follows:

    #!/usr/bin/perl print "Content-type: text/html"; print "<html>\n<head>\n<title>Test FTP Sclar</title>\n</head>\n<body>\ +n<p>___ hier gehts los ___:</p>\n<p>\n"; use Net::FTP; my $content = <<__CONTENT__; <inno> <antrag> <tester>THOFMANN(TH)</tester> </antrag> <hinweis> <ziel>Test-Server</ziel> </hinweis> </inno> __CONTENT__ my $server = 'www.lly5.de'; my ($login, $passwd) = ('theoneandonly', 'g1v3m3s0m35h17'); my $localdir = "d:/Dokumente2/Server2/cgi-bin/out/"; my $datei = "test-ftpscalar.pl"; $datei = "test-ftpscalar.xml"; my $remotefile = ''; my $remotefilename; my $passive; my $remotedir = 'public_html/inno/'; my $localfilename; $localfilename = "$localdir\/$datei"; if ($remotefile ne '') { $remotefilename = $remotefile; } else { $remotefilename = $datei; } if ($server =~ m/(www.lly5.de)/i) { # fies, name wurde i +n IP geaendert $passive = 1; $ftp = Net::FTP->new($server, Passive => 1) or die "Cannot connect to $server (Passive=$passiv +e): $@"; } else { $ftp = Net::FTP->new($server) or die "Cannot connect to $server: $@"; } open( FH, "<", \$content) or die "Kann File Handle nic +ht zum lesen aus content oeffnen"; $ftp->login($login, $passwd) or die "Cannot login (Passive=$passive) ", $ftp->m +essage; $ftp->cwd($remotedir) or die "Cannot change working directory (Passive=$ +passive) ", $ftp->message; $ftp->binary or die "Cannot change to binary mode (Passive=$pas +sive) ", $ftp->message; $ftp->put(FH, $remotefilename) or die "put failed (Passive=$passive) ", $ftp->mes +sage, "; localfilename(FH): [$localfilename], remotefilename: [$remot +efilename]"; $ftp->quit; close( FH ); print "*** Fertig FTP ***\n";

    Thanks in advance, Thomas

      Try using a lexical filehandle

      open( my $FH, "<", \$content ) or die "Kann File Handle nicht zum lesen aus content oeffnen"; . . $ftp->put( $FH, $remotefilename ) or die "put failed (Passive=$passive) ", $ftp->message, "; local +filename(FH): [$localfilename], remotefilename: [$remotefilename]"; $ftp->quit; close( $FH );
      poj

        Dear poj,

        many thanks, this works in my test script now. will try to move this to my live script.

        Thanks again and best regards, Thomas

        PS: I guess a :

        binmode( $FH );

        ... after open will be processed correctly