Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Uploading and reading file

by Anonymous Monk
on Nov 22, 2011 at 16:45 UTC ( #939496=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to upload a file, and then process the contents of the file. Mostly this works fine, but in some cases, the results on the page are incorrect. It prints the processed filename correctly, but the processed contents are the contents from a previously uploaded file.

Any ideas where it goes wrong ?

Thanks! Rianne
use CGI qw/:standard -private_tempfiles/; use Text::CSV; use Fcntl qw( :DEFAULT :flock ); use constant UPLOAD_DIR => "/tmp"; use constant BUFFER_SIZE => 16_384; use constant MAX_FILE_SIZE => 50*1_048_576; # Limit each upload + to 50 MB use constant MAX_DIR_SIZE => 100 * 1_048_576; # Limit total uploads +to 100 MB use constant MAX_OPEN_TRIES => 100; $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = MAX_FILE_SIZE; my $q = new CGI; $q->cgi_error and error( $q, "Error transferring file: " . $q->cgi_err +or ); #Some other stuff here print $q->header( "text/html", -expires => "-1s" ), + $q->start_html( -title => "Wordcloud Generator", -bgcolor => "#f +fffff", -head=>$q->meta({-http_equiv => 'Pragma', -content => 'no-cache'}) +), $q->h2( "Wordcloud Generator" ), $q->hr, $q->p("Processed file:", $file); my $in = $q->upload( "file" ); my $csv = Text::CSV->new ({ binary => 1, eol => $/, sep_char => "\t" } +); my $header = "1"; my $numcolumns; while(my $line = <$in>) { if($csv->parse($line)) { my @row = $csv->fields(); #processing the file

Comment on Uploading and reading file
Download Code
Re: Uploading and reading file
by MidLifeXis (Prior) on Nov 22, 2011 at 17:52 UTC

    Start with use strict;, use warnings;, and possibly use diagnostics;. You may have just been too aggressive in your trimming code, but I don't see where you declare $file before using it.

    See also the section on Embedded newlines -- your line processing is fragile.

    It is useful when you ask for help to make it easy for us to replicate your issue. Please include sample input, expected output, actual output, and code that compiles cleanly and illustrates your issue.

    --MidLifeXis

Re: Uploading and reading file
by Khen1950fx (Canon) on Nov 22, 2011 at 19:41 UTC
    Here's what I found. Make sure that you run it with -T on the cmdline. I left out the csv section to concentrate on CGI.
    #!/usr/bin/perl -lT use strict; use Fcntl qw/:DEFAULT :flock/; use CGI::Carp qw/fatalsToBrowser/; use CGI qw/:standard -private_tempfiles/; $CGI::POST_MAX = 1024 * 1024 * 50; $CGI::DISABLE_UPLOADS = 0; use constant UPLOAD_DIR => '/tmp'; use constant BUFFER_SIZE => 16384; use constant MAX_DIR_SIZE => 1024 * 1024 * 100; use constant MAX_OPEN_TRIES => 100; my $q = new CGI; print $q->header( "text/html", -expires => "-1s" ), $q->start_html( -title => "Wordcloud Generator", -bgcolor => "#ffffff", -head => $q->meta( { -http_equiv => 'Pragma', -content => 'no-cache' } ) ), $q->h2("Wordcloud Generator"), $q->hr, $q->p( "Processed file:", my $file ), $q->end_html; my $in = $q->upload('file'); $in = param('upload'); if (not $in && cgi_error()) { print $q->header(-status => cgi_error()); exit 0; }
Re: Uploading and reading file
by Anonymous Monk on Nov 23, 2011 at 14:49 UTC
    Thanks for the help. The issue actually had to do with Modperl. Found a solution here: http://perl.apache.org/docs/general/perl_reference/perl_reference.html I changed some of the my declarations to local our, and now it works fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://939496]
Approved by Eliya
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-12-18 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls