File name from a form

Hi all can you help

I am trying to get a CGI script to create a file name from a from

I want to create an output file within the /tmp dir called Xxxx.conf
Xxxx would be the inputted value from the form
Here is my code minus some bits

#!/usr/bin/perl

# Redirection URL if required
$redirect_url = "";

# Output file
$form_name= "host_id" ; # what would I put in here  print ?
$output_file = "/tmp/$form_name_" ;

#############################################################################

# Parse Form Contents
&parse_form;

# Write to output file
&write_to_file;

# Return HTML Page or Redirect User
&return_html;

sub parse_form {
    # Parse the incoming data depending on request method
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        # Split the name-value pairs
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        # Read the input
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 
        # Split the name-value pairs
        @pairs = split(/&/, $buffer);

# THE BIT THAT SLITS UP THE FORM.
.
.
sub write_to_file {
     if (defined($Form{'build_environment'})) {
          open FH, ">" . $output_file;

          print FH "host_id:" . $Form{' host_id t'} . "\n";

close FH;
     }
}
LVL 1
webstepAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jbuhacoffConnect With a Mentor Commented:
I forgot you wanted .conf at the end of the file name.  Change this line:

$output_file =  "/tmp/" . $Form{'host_id'};


To this one:

$output_file =  "/tmp/" . $Form{'host_id'} . ".conf";

And that will do it for you.
0
 
naminator2Commented:
i think u can use cgi.pm which u can download here
http://stein.cshl.org/WWW/software/CGI/

i always use this cause i think it is so much easier than doing it by hand.
hope this helps
0
 
amitabhraiCommented:
I am pasting a perl script here that will do the job...
It assumes that you are passing only one file to the server
and no other input fields from the html. You can change the code if you want to accomodate more input variables.
I have tested the script against IE and Apache server and it worked fine.
=====================
#!/bin/perl

print "Content-type: text/html\n\n";

binmode STDIN;
while ( <STDIN> ) {
     $line = $_ ;
     $line =~ s/^M//g ;
     chop $line;
     if ( $line =~ /filename=/i ) {
          $inputFileName = substr( $line, index ( $line , "filename=" ) );
          $inputFileName =~ s/^.*\\// ;
          $inputFileName =~ s/\"// ;
     }
     if( $line =~ /^$/ ) {
          last ;
     }
}

$outputFile = "/tmp/" . $inputFileName . ".conf" ;

open ( OUTPUT , ">$outputFile" );
binmode OUTPUT;

while ( <STDIN> ) {
     $line = $_ ;
     $line =~ s/^M//g ;
     if( $line =~ /------------/ ) {
          last;
     }
     print OUTPUT $line ;
}

close OUTPUT ;

print "\n$outputFile File Uploaded";

=========================================
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
webstepAuthor Commented:
Do i have to go through all that to get the value out of  a form

sub write_to_file {
    if (defined($Form{'build_environment'})) {
         open FH, ">" . $output_file;

         print FH "host_id:" . $Form{'host_id'} . "\n";

close FH;
    }
}
if some thing like the above prints to the output file could i not re use $Form{'host_id'}  again for the file name ?
0
 
amitabhraiCommented:
webstep,
     The script I included before is the crude way of implementing what you wanted and it does work. But sorry I don't know what is "$Form{'build_environment'}" and
"$Form{'host_id'}" that you are using.
0
 
webstepAuthor Commented:
Sorry as I totally new to scripting and was trying to get this peice of code to work


From what I can read from the code the form send the data  and then outputted to the screen

All I wish to do it grab the host id variable and put it as the file name


#!/usr/bin/perl

# Redirection URL if required
$redirect_url = "";

# Output file
$form_name= "host_id" ;
$output_file = "/tmp/$form_name_" ;



#############################################################################

# Parse Form Contents
&parse_form;

# Write to output file
&write_to_file;

# Return HTML Page or Redirect User
&return_html;

sub parse_form {
    # Parse the incoming data depending on request method
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        # Split the name-value pairs
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        # Read the input
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 
        # Split the name-value pairs
        @pairs = split(/&/, $buffer);
    }

    foreach $pair (@pairs) {
        # Split the pair up into individual variables.
        local($name, $value) = split(/=/, $pair);
 
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $name =~ tr/\0//d;

        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/\0//d;

        if ($Form{$name} ne '') {
            $Form{$name} = "$Form{$name}, $value";
        } else {
            push(@Field_Order,$name);
            $Form{$name} = $value;
        }
    }
}



sub return_html {
      if ($redirect_url) {
            print "Location: " . $redirect_url;
      } else {
        print "Content-type: text/html\n\n";
        print "<html>\n <head>\n";

        print "<title>CGI Form Processor</title>\n";
        print " </head>\n <body>\n";
 
            print "<h1>File Cretaed </h1>\n";
            print "<br/><br/>\n";
            print "MYEST" . $Form{'form_name'} . "<br/>\n";


            print "build_environment:" . $Form{'build_environment'} . "<br/>\n";
            print "host_id:" . $Form{'host_id'} . "<br/>\n";
            print "part_prim_0_root:"  . $Form{'part_prim_0_root'} . "<br/>\n";
            print "part_prim_1_swap:" . $Form{'part_prim_1_swap'} . "<br/>\n";
            print "part_prim_3_usr:" . $Form{'part_prim_3_usr'} . "<br/>\n";
            print "part_prim_4_var:" . $Form{'part_prim_4_var'} . "<br/>\n";
            print "part_prim_5_opt:" . $Form{'part_prim_5_opt'} . "<br/>\n";
            print "part_prim_7_metadb:" . $Form{'part_prim_7_metadb'} . "<br/>\n";
            print "part_sec_0_root:" . $Form{'part_sec_0_root'} . "<br/>\n";
            print "part_sec_1_swap:" . $Form{'part_sec_1_swap'} . "<br/>\n";
            print "part_sec_3_usr:" . $Form{'part_sec_3_usr'} . "<br/>\n";
            print "part_sec_4_var:" . $Form{'part_sec_4_var'} . "<br/>\n";
            print "part_sec_5_opt:" . $Form{'part_sec_5_opt'} . "<br/>\n";
            print "part_sec_7_metadb:" . $Form{'part_sec_7_metadb'} . "<br/>\n";
            print "nodename:" . $Form{'nodename'} . "<br/>\n";
            print "alias_1:" . $Form{'alias_1'} . "<br/>\n";
            print "alias_2:" . $Form{'alias_2'} . "<br/>\n";
            print "primary_ip_address:" . $Form{'primary_ip_address'} . "<br/>\n";
            print "primary_ip_default_router:" . $Form{'primary_ip_default_router'} . "<br/>\n";
            print "primary_ip_default_netmask:" . $Form{'primary_ip_default_netmask'} . "<br/>\n";
            print "secondary_ip_default_router:" . $Form{'secondary_ip_default_router'} . "<br/>\n";
            print "secondary_ip_address:" . $Form{'secondary_ip_address'} . "<br/>\n";
            print "secondary_ip_default_netmask:" . $Form{'secondary_ip_default_netmask'} . "<br/>\n";
            print "ets_network:" . $Form{'ets_network'} . "<br/>\n";
            print "sr_host_or_network_1:" . $Form{'sr_host_or_network_1'} . "<br/>\n";
            print "sr_network_ip_1:" . $Form{'sr_network_ip_1'} . "<br/>\n";
            print "sr_gateway_ip_1:" . $Form{'sr_gateway_ip_1'} . "<br/>\n";
            print "sr_subnet_mask_1:" . $Form{'sr_subnet_mask_1'} . "<br/>\n";
            print "sr_host_or_network_2:" . $Form{'sr_host_or_network_2'} . "<br/>\n";
            print "sr_network_ip_2" . $Form{'sr_network_ip_2'} . "<br/>\n";
            print "sr_gateway_ip_2:" . $Form{'sr_gateway_ip_2'} . "<br/>\n";
            print "sr_subnet_mask_2:" . $Form{'sr_subnet_mask_2'} . "<br/>\n";





            print "</body>\n</html>\n";
    }
}

sub write_to_file {
      if (defined($Form{'build_environment'})) {
            open FH, ">" . $output_file;

            print FH "build_environment:" . $Form{'build_environment'} . "\n";
            print FH "host_id:" . $Form{'host_id'} . "\n";
            print FH "part_prim_0_root:" . $Form{'part_prim_0_root'} . "\n";
            print FH "part_prim_1_swap:" . $Form{'part_prim_1_swap'} . "\n";
            print FH "part_prim_3_usr:" . $Form{'part_prim_3_usr'} . "\n";
            print FH "part_prim_4_var:" . $Form{'part_prim_4_var'} . "\n";
            print FH "part_prim_5_opt:" . $Form{'part_prim_5_opt'} . "\n";
            print FH "part_prim_7_metadb:" . $Form{'part_prim_7_metadb'} . "\n";
            print FH "part_sec_0_root:" . $Form{'part_sec_0_root'} . "\n";
            print FH "part_sec_1_swap:" . $Form{'part_sec_1_swap'} . "\n";
            print FH "part_sec_3_usr:" . $Form{'part_sec_3_usr'} . "\n";
            print FH "part_sec_4_var:" . $Form{'part_sec_4_var'} . "\n";
            print FH "part_sec_5_opt:" . $Form{'part_sec_5_opt'} . "\n";
            print FH "part_sec_7_metadb:" . $Form{'part_sec_7_metadb'} . "\n";
            print FH "nodename:" . $Form{'nodename'} . "\n";
            print FH "alias_1:" . $Form{'alias_1'} . "\n";
            print FH "alias_2:" . $Form{'alias_2'} . "\n";
            print FH "primary_ip_address:" . $Form{'primary_ip_address'} . "\n";
            print FH "primary_ip_default_router:" . $Form{'primary_ip_default_router'} . "\n";
            print FH "primary_ip_default_netmask:" . $Form{'primary_ip_default_netmask'} . "\n";
            print FH "secondary_ip_default_router:" . $Form{'secondary_ip_default_router'} . "\n";
            print FH "secondary_ip_address:" . $Form{'secondary_ip_Address'} . "\n";
            print FH "secondary_ip_default_netmask:" . $Form{'secondary_ip_default_netmask'} . "\n";
            print FH "ets_network:" . $Form{'ets_network'} . "\n";
            print FH "sr_host_or_network_1:" . $Form{'sr_host_or_network_1'} . "\n";
            print FH "sr_network_ip_1:" . $Form{'sr_network_ip_1'} . "\n";
            print FH "sr_gateway_ip_1:" . $Form{'sr_gateway_ip_1'} . "\n";
            print FH "sr_subnet_mask_1:" . $Form{'sr_subnet_mask_1'} . "\n";
            print FH "sr_host_or_network_2:" . $Form{'sr_host_or_network_2'} . "\n";
            print FH "sr_network_ip_2" . $Form{'sr_network_ip_2'} . "\n";
            print FH "sr_gateway_ip_2:" . $Form{'sr_gateway_ip_2'} . "\n";
            print FH "sr_subnet_mask_2:" . $Form{'sr_subnet_mask_2'} . "\n";
                  

            close FH;
      }
}

0
 
jbuhacoffCommented:
All you need to do is replace the following block:

# Output file
$form_name= "host_id" ;
$output_file = "/tmp/$form_name_" ;

with this one:

# Output file
$output_file =  "/tmp/" . $Form{'host_id'};

And then move it from where it is to right in between the &parse_form and &write_to_file blocks, so that your file looks more like this:

# Parse Form Contents
&parse_form;

# Output file
$output_file =  "/tmp/" . $Form{'host_id'};

# Write to output file
&write_to_file;

0
All Courses

From novice to tech pro — start learning today.