Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

CGI, file upload and netscape 7

by aging acolyte (Pilgrim)
on Nov 20, 2002 at 11:28 UTC ( [id://214399] : perlquestion . print w/replies, xml ) Need Help??

aging acolyte has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am stumpted and submit myself to your mercy. I suspect my problem may be with my browser but I cannot pass a file upload parameter to my cgi script with the full directory path. The file parameter is always local to my cgi-bin and not the full path the documentation implies it should be. The code works in netscape 6. Any ideas? Thanks AA
#!/usr/bin/perl -w -T use strict; use CGI; my $entrypg=new CGI; print $entrypg->header, $entrypg->start_html( -title => "Load your file", -BGCOLOR => 'white', -LINk => 'red' ), $entrypg->center($entrypg->h1("Upload annotation file")), $entrypg->start_multipart_form( -name=>'form', -method=>'POST', -action=>'' ), $entrypg->table( {-border=>'1', -align=>'CENTER', -cellspacing=> '1', -cellpadding=>'1', }, $entrypg->TR( {-colspan=>'2'}, "Please choose your file:" ), $entrypg->TR( $entrypg->td($entrypg->filefield( -name=>'INPUT', -size=>'50' ) ) ) ), $entrypg->submit, $entrypg->reset;
and Parse.cgi=
#!/usr/bin/perl -w -T use strict; use CGI; use Data::Dumper; use XML::Parser; my $xmlpg = new CGI; my $file = $xmlpg->param("INPUT"); my ($ID,$from, $to); my $p =new XML::Parser(Style=>'Tree'); my $doc = $p->parsefile("$file"); #print Dumper($doc); my $level =0; process_node(@$doc); sub process_node { my ($type, $content) = @_; if ($type eq 'id') { $ID = trim($content->[2]); } elsif ($type eq 'from') { $from = trim($content->[2]); } elsif ($type eq 'to') { $to = trim($content->[2]); } if ($type) { while (my @node = splice @$content, 1, 2) { process_node(@node) } } } sub trim { local $_ = shift; s/\n/ /g; s/^\s+//; s/\s+$//; return $_; } print $xmlpg ->header, $xmlpg->start_html( -title => "XML File", -BGCOLOR => 'white', -LINk => 'red' ), $xmlpg->h1("Looking at $ID which starts at $from and finishes at + $to");

Replies are listed 'Best First'.
CGI, file upload and netscape 7
by aging acolyte (Pilgrim) on Nov 21, 2002 at 11:57 UTC

    I have found a work around but not an ideal solution.

    The problem is that param("INPUT") is being read as a file handle and so causes problems when passed into XML::Parser.

    My solution is to write the open file into a tmp file and then pass that tmp file to the parser.

    Can anyone suggest a more elegant solution?

    Thanks again