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

I need to write or create a script in Perl that will take a posted form field value, store it as a string and then save that value via FTP onto my shared web hosting account. Ideally I would like this script to work in both Unix and Windows Perl environments.

I am assuming that Net::FTP might be the way to go, but not sure how to use the string value stored in memory to write the file via FTP. Then again this module might be disabled in some shared environments so maybe I will need to write directly to the command line and have two different branches depending on if the environment is Unix or Windows.

Thanks for the help.

  • Comment on Using Perl FTP to write a string variable as a file

Replies are listed 'Best First'.
Re: Using Perl FTP to write a string variable as a file
by amw1 (Friar) on Dec 27, 2004 at 20:15 UTC
    The perldoc for Net::FTP says that you can give put a filehandle. All you'd have to do is something like this:
    open(FH, "<", \$string) or die "Whoops!"; $ftp->put(FH, "remote_file.name");
    I have not tested this at all.
    Update: Added \$string as per Roy.
      I think you want \$string there. Opening a reference to a variable reads the string in the variable as if it were a file. If you just have a scalar variable, it will try to open a file by the name of whatever is in the variable.

      Caution: Contents may have been coded under pressure.
      I know this is an old post just thought I'd comment that we needed to do this, so we tried the above code and it worked flawlessly.

      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

        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

Re: Using Perl FTP to write a string variable as a file
by Zaxo (Archbishop) on Dec 27, 2004 at 20:17 UTC

    If the ftp account is on the same host and account as the web server, why not just write the string to a file? CGI.pm makes that really easy with its save method. This does that,

    use CGI; my $q = CGI->new; open my $fh, '>>', '/path/to/requests.log' or die $!; $q->save($fh); close $fh or die $!;
    I'll leave printing the html responses to the remote client as an exercise ;-)

    You can exhibit the output of perldoc perllocal to see which modules are installed by standard EU::MM methods.

    After Compline,
    Zaxo

Re: Using Perl FTP to write a string variable as a file
by dragonchild (Archbishop) on Dec 27, 2004 at 20:20 UTC
    • What have you tried? Do you even have pseudo-code?
    • What experience do you have with Net::FTP? Have you tried a simple script of writing a random string to a remote file?
    • Why are you writing a script that doesn't know what host it will be on? If you're on multiple hosts, doesn't that mean you are going to control them?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      # What have you tried? Do you even have pseudo-code?

      My first attempt was to do this with ASP and eventualy PHP. But when I realized writing text files to the folders displaying the web files generated errors due to lack of permission I realized FTP should be a quick work around.

      I do not have any pseudo-code to help anyone help me out. I think the first response looks promising and if it works may be all I need.

      # What experience do you have with Net::FTP? Have you tried a simple script of writing a random string to a remote file?

      I have no experience with Net::FTP but based on what I read I think it's the way to go. I have not attempted to write a random string to a remote file. If I could find an example of this then I think I will have the information I need to complete my task.

      # Why are you writing a script that doesn't know what host it will be on? If you're on multiple hosts, doesn't that mean you are going to control them?

      Once I have created the script I would like to make it available to as many people as possible. Since most Unix and Windows hosts come pre-installed with Perl then it is very likely that a Perl script could be an ideal cross platform solution.

Re: Using Perl FTP to write a string variable as a file
by TedPride (Priest) on Dec 27, 2004 at 20:17 UTC
    Why do you need FTP? Do you have two hosting accounts, one of which is receiving form input and the second the data in a file? Wouldn't it be simpler to run the script directly on the target hosting account and write the data directly to a local file?

    Just puzzled. Some further explanation might be useful.

      Thanks for all the quick responses.

      A little more background information:

      The script that I am creating grabs live web pages being served in the clients section, replaces a segment of code and then needs to rewrite the file with the changes.

      Since the files are stored in the web display folder where the permissions are resonably strict the only sure fire way I know of to write the files without having to worry about permissions is to use FTP.

      This assumption may be inaccurate. If it is I would be all too happy to use standard commands to write the HTML file. But I fear that the permissions in the web display folder will not allow my script to do so.