Nicholas Niggel
asked on
cgi upload file and count words
Hello
I am a student working on homework. Prob is get a file with cgi from html page, upload file and count words, display results. I'm sure its a common hw prob but I am stuck. Also, have checkbox to upload automatically a file for testing "raven.txt" if checked
Any help much appreciated, please explain my mistake.
cgi is below also html below that
!/usr/bin/perl
#file: upload.pl
use CGI qw/:standard/;
print header,
start_html('Word Counter'),
h1('Word Counter');
print_form() unless param;
print_results() if param;
print end_html;
sub print_form {
print start_multipart_form(),
filefield(-name=>'upload', -size=>60) ,br,
submit(-label=>'Upload File'),
end_form;
}
sub print_results {
my $length;
my $file = param('upload');
if (!$file) {
print "No file uploaded.";
return;
}
print h2("File name\n"),$file;
my $lines = 0;
my $words = 0;
my @words;
my @output;
@lines = @_;
foreach (@lines){
$lines++;
@words = split;
foreach(@words){
$words++;
}
}
print "<br><b>Number of Lines:</b> $lines <br>\n";
print "<b>Number of Words:</b> $words <br>\n";
}
# html
<html>
<title>Basic Word Counter</title>
<body>
<h1>Word Counting Program</h1>
What file would you like to process
<form action="/cgi-bin/word.cgi" method="post" enctype="multipart/form-da ta">
File Name:
<br>
<input type="file" name="upload" size="40">
<br>
<br>
<input type="reset" label="RESET">
<input type="submit" name=".submit" value="Count Words">
<input type="hidden" name=".cgifields" value="raven">
<br><br>
<input type="checkbox" name="raven" value="on">
Or choose
<a href="raven.txt">The Raven</a>
</form>
</body>
</html>
I am a student working on homework. Prob is get a file with cgi from html page, upload file and count words, display results. I'm sure its a common hw prob but I am stuck. Also, have checkbox to upload automatically a file for testing "raven.txt" if checked
Any help much appreciated, please explain my mistake.
cgi is below also html below that
!/usr/bin/perl
#file: upload.pl
use CGI qw/:standard/;
print header,
start_html('Word Counter'),
h1('Word Counter');
print_form() unless param;
print_results() if param;
print end_html;
sub print_form {
print start_multipart_form(),
filefield(-name=>'upload',
submit(-label=>'Upload File'),
end_form;
}
sub print_results {
my $length;
my $file = param('upload');
if (!$file) {
print "No file uploaded.";
return;
}
print h2("File name\n"),$file;
my $lines = 0;
my $words = 0;
my @words;
my @output;
@lines = @_;
foreach (@lines){
$lines++;
@words = split;
foreach(@words){
$words++;
}
}
print "<br><b>Number of Lines:</b> $lines <br>\n";
print "<b>Number of Words:</b> $words <br>\n";
}
# html
<html>
<title>Basic Word Counter</title>
<body>
<h1>Word Counting Program</h1>
What file would you like to process
<form action="/cgi-bin/word.cgi"
File Name:
<br>
<input type="file" name="upload" size="40">
<br>
<br>
<input type="reset" label="RESET">
<input type="submit" name=".submit" value="Count Words">
<input type="hidden" name=".cgifields" value="raven">
<br><br>
<input type="checkbox" name="raven" value="on">
Or choose
<a href="raven.txt">The Raven</a>
</form>
</body>
</html>
ASKER
OK while you where answering I did this with $ARGV it now counts choosen file
What about the checkbox for auto uploading?
#!/usr/bin/perl
#file: upload.pl
use CGI qw/:standard/;
print header,
start_html('Word Counter'),
h1('Word Counter');
print_form() unless param;
print_results() if param;
print end_html;
sub print_form {
print start_multipart_form(),
filefield(-name=>'upload', -size=>60) ,br,
submit(-label=>'Upload File'),
end_form;
}
sub print_results {
my $length;
my $file = param('upload');
if (!$file) {
$file="raven.txt";
open(FILE, "$file") || die "Cannt: $!\n";
while (<FILE>) {
$lines{$ARGV}++;
@words = split(/\W+/);
$count{ARGV} += @words;
}
foreach $linecount (sort keys %lines) {
print h2('File Lines'), $lines{$linecount};
}
foreach $wordcount (sort keys %count) {
print h2('File Words'), $count{$wordcount};
}
}
else {
while (<$file>) {
$lines{$ARGV}++;
@words = split(/\W+/);
$count{$ARGV} += @words;
}
foreach $linecount2 (sort keys %lines) {
print h2('File Lines'), $lines{$linecount2};
}
foreach $wordcount2 (sort keys %count) {
print h2('File Word'), $count{$wordcount2};
}
}
}
What about the checkbox for auto uploading?
#!/usr/bin/perl
#file: upload.pl
use CGI qw/:standard/;
print header,
start_html('Word Counter'),
h1('Word Counter');
print_form() unless param;
print_results() if param;
print end_html;
sub print_form {
print start_multipart_form(),
filefield(-name=>'upload',
submit(-label=>'Upload File'),
end_form;
}
sub print_results {
my $length;
my $file = param('upload');
if (!$file) {
$file="raven.txt";
open(FILE, "$file") || die "Cannt: $!\n";
while (<FILE>) {
$lines{$ARGV}++;
@words = split(/\W+/);
$count{ARGV} += @words;
}
foreach $linecount (sort keys %lines) {
print h2('File Lines'), $lines{$linecount};
}
foreach $wordcount (sort keys %count) {
print h2('File Words'), $count{$wordcount};
}
}
else {
while (<$file>) {
$lines{$ARGV}++;
@words = split(/\W+/);
$count{$ARGV} += @words;
}
foreach $linecount2 (sort keys %lines) {
print h2('File Lines'), $lines{$linecount2};
}
foreach $wordcount2 (sort keys %count) {
print h2('File Word'), $count{$wordcount2};
}
}
}
very good :-)
yes, how about that checkbox...
You have to add it to your form of course, so the user can see it, and check it if they want.
Then you will have to check if it has been selected, and act accordingly.
yes, how about that checkbox...
You have to add it to your form of course, so the user can see it, and check it if they want.
Then you will have to check if it has been selected, and act accordingly.
ASKER
kandura - you still out there ? Or anyone else ?
Can you take a look at this and give me a pointer as to why my print_raven doesn't count words or lines ?
#!/usr/bin/perl
use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
print_form() unless param;
print_results() if param ('upload');
print_raven() if param ('The Raven');
print end_html;
sub print_form {
print header;
print start_html(-title=>'Basic Word Counter');
print h1("Basic Word Counter");
print start_multipart_form();
print "Please upload a file";
print br;
print filefield(-name=>'upload', -size=>40);
print br;
print checkbox(-name=>'The Raven');
print br;
print br;
print submit(-label=>'GO');
print reset;
print endform;
}
sub print_results {
my $file = param('upload');
print header;
print start_html;
print h1("The file you selected has:");
print br;
while (<$file>) {
$lines{$ARGV}++;
@words = split(/\W+/);
$count{$ARGV} += @words;
}
foreach $linecount2 (sort keys %lines) {
print h2('File Lines'), $lines{$linecount2};
}
foreach $wordcount2 (sort keys %count) {
print h2('File Word'), $count{$wordcount2};
}
print h2('File Name'), $file;
}
sub print_raven {
my $file = 'raven.txt' ;
print header;
print start_html;
print h1("The Raven has:");
print br;
while (<$file>) {
$lines++;
@words = split(/\W+/);
$count += @words;
}
foreach $linecount2 (sort keys %lines) {
print h2('File Lines'), $lines{$linecount2};
}
foreach $wordcount2 (sort keys %count) {
print h2('File Word'), $count{$wordcount2};
}
print h2('File Name'), $file;
}
Can you take a look at this and give me a pointer as to why my print_raven doesn't count words or lines ?
#!/usr/bin/perl
use CGI ':standard';
use CGI::Carp 'fatalsToBrowser';
print_form() unless param;
print_results() if param ('upload');
print_raven() if param ('The Raven');
print end_html;
sub print_form {
print header;
print start_html(-title=>'Basic Word Counter');
print h1("Basic Word Counter");
print start_multipart_form();
print "Please upload a file";
print br;
print filefield(-name=>'upload',
print br;
print checkbox(-name=>'The Raven');
print br;
print br;
print submit(-label=>'GO');
print reset;
print endform;
}
sub print_results {
my $file = param('upload');
print header;
print start_html;
print h1("The file you selected has:");
print br;
while (<$file>) {
$lines{$ARGV}++;
@words = split(/\W+/);
$count{$ARGV} += @words;
}
foreach $linecount2 (sort keys %lines) {
print h2('File Lines'), $lines{$linecount2};
}
foreach $wordcount2 (sort keys %count) {
print h2('File Word'), $count{$wordcount2};
}
print h2('File Name'), $file;
}
sub print_raven {
my $file = 'raven.txt' ;
print header;
print start_html;
print h1("The Raven has:");
print br;
while (<$file>) {
$lines++;
@words = split(/\W+/);
$count += @words;
}
foreach $linecount2 (sort keys %lines) {
print h2('File Lines'), $lines{$linecount2};
}
foreach $wordcount2 (sort keys %count) {
print h2('File Word'), $count{$wordcount2};
}
print h2('File Name'), $file;
}
That's because you do not have the hashes %lines or %count.
The number of lines is in $lines and the word count is in $count.
So remove the two foreach blocks from print_raven and add instead:
print h2('File Lines'), $lines;
print h2('File Word'), $count;
The number of lines is in $lines and the word count is in $count.
So remove the two foreach blocks from print_raven and add instead:
print h2('File Lines'), $lines;
print h2('File Word'), $count;
ASKER
Thanks for getting back to me.
I see my use of the hashes instead of scaler(thanks for pointing that out), but I tried your suggestion and it still doesn't show a number count for words and lines.
Is this correct:
while (<$file>) {
$lines++;
@words = split(/\W+/);
$count += @words;
}
I have the file raven.txt in the same directory.
I see my use of the hashes instead of scaler(thanks for pointing that out), but I tried your suggestion and it still doesn't show a number count for words and lines.
Is this correct:
while (<$file>) {
$lines++;
@words = split(/\W+/);
$count += @words;
}
I have the file raven.txt in the same directory.
Yes, that looks about ok. However, there may be a problem opening the file. Make sure that the webserver user has permission to read it.
Try opening it explicitly to see if that's the problem:
sub print_raven {
my $file = 'raven.txt' ;
print header;
print start_html;
print h1("The Raven has:");
print br;
open F, "$file" or print "<b>Error opening $file: $!</b><br>"; # <---- this might give you a clue
while (<F>) {
$lines++;
@words = split(/\W+/);
$count += @words;
}
close F;
print h2('File Lines'), $lines;
print h2('File Word'), $count;
print h2('File Name'), $file;
}
Try opening it explicitly to see if that's the problem:
sub print_raven {
my $file = 'raven.txt' ;
print header;
print start_html;
print h1("The Raven has:");
print br;
open F, "$file" or print "<b>Error opening $file: $!</b><br>"; # <---- this might give you a clue
while (<F>) {
$lines++;
@words = split(/\W+/);
$count += @words;
}
close F;
print h2('File Lines'), $lines;
print h2('File Word'), $count;
print h2('File Name'), $file;
}
ASKER
Well that did the trick. It works with that.
must have been this
open F, "$file"
I was trying to read the file without opening it. Do I need to open it with an open statement first ?
must have been this
open F, "$file"
I was trying to read the file without opening it. Do I need to open it with an open statement first ?
ASKER
How about this.
WHen I hit the GO button without selecting a file and without checking the checkbox. I get an error
Error message:
Premature end of script headers: word.cgi
it should just call the print_form() routine from this
print_form() unless param;
Any ideas ?
WHen I hit the GO button without selecting a file and without checking the checkbox. I get an error
Error message:
Premature end of script headers: word.cgi
it should just call the print_form() routine from this
print_form() unless param;
Any ideas ?
About opening: yes, in this case you should. The upload field gives you an already opened filehandle to the uploaded file.
About form: I think the submit button also sends a parameter (usually its name). The best thing you can do is check if the form was posted.
If it wasn't, chances are that $ENV{'REQUEST_METHOD'} eq 'GET', while it will definitely be 'POST' when the form was submitted.
Otherwise, check on the explicit parameters you need.
About form: I think the submit button also sends a parameter (usually its name). The best thing you can do is check if the form was posted.
If it wasn't, chances are that $ENV{'REQUEST_METHOD'} eq 'GET', while it will definitely be 'POST' when the form was submitted.
Otherwise, check on the explicit parameters you need.
ASKER
The form is a post. Would I check this in the cgi by looking for a param ? like
print_form() if param ('GO');
I tried that and no good.
I thought that
print_form() unless param;
would do it
print_form() if param ('GO');
I tried that and no good.
I thought that
print_form() unless param;
would do it
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
kandura That's what I needed.
Thanks for all the Help.
Thanks for all the Help.
Once you find the difference between those two, you will have your answer :-)
Kandura