?
Solved

File name  from a form

Posted on 2003-02-24
7
Medium Priority
?
277 Views
Last Modified: 2013-12-25
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;
     }
}
0
Comment
Question by:webstep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 

Expert Comment

by:naminator2
ID: 8015177
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
 

Expert Comment

by:amitabhrai
ID: 8018738
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
 
LVL 1

Author Comment

by:webstep
ID: 8020147
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Expert Comment

by:amitabhrai
ID: 8020378
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
 
LVL 1

Author Comment

by:webstep
ID: 8021501
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
 
LVL 1

Expert Comment

by:jbuhacoff
ID: 10397056
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
 
LVL 1

Accepted Solution

by:
jbuhacoff earned 80 total points
ID: 10397075
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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question