Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Howdy Folks,

I have been out of the perl game for a while now, but with the help of another monk, we have the script below. Essentially what I'm trying to pull off is uploading a spreadsheet and converting it to xml. After the upload is complete, I get an error stating the CGI did not produce a valid header. I have tried to play with it but can't seem to find a solution and feel like I'm grabbing at straws. Can anyone shed some light on my problem? Thanks!

UPDATE: Here is the error from the logs:
failure ( 171): for host ***.***.**.*** trying to POST /cgi-bin/upload +.cgi, cgi_scan_headers reports: HTTP4044: the CGI program /cgi-bin/up +load.cgi did not produce a valid header (program termindated without +a vaild CGI header.


UPDATE2: Bare with me here, I've been out of the game for 2 years now! Anyway, I have everything working up until the xSV_to_XML sub and I was getting an $die error on the files. I think I'm using exec correctly and not supposed to use system() to change the permissions on the files. The script just dies when it get here, no error to the browser or the log. How can I ensure that I can read/write the files?
my $IN = $upload_path . param('excel') . ".txt"; exec "chmod 775 $IN"; my $OUT = "master.xml"; exec "chmod 775 master.xml";


The Beast:
#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); $CGI::POST_MAX = 2097152; ## (2mb upload max) $CGI::DISABLE_UPLOADS = 0; ## allow uploads my $upload_path = "/export/"; my $filename = $upload_path . param('excel'); sub print_header { my $title = shift; print header, start_html($title); } sub init { my $upload = upload('excel'); unless (param('excel')) { print_header ('Excel to XML Converter'); print_upload_form(); ## make sure there's actually something being uploaded } elsif (!$upload && cgi_error) { print_header ('Excel to XML Converter: Error!'); print h1("No file uploaded!"); ## all is well, show conversion confirmation } else { show_convert(); } } sub print_upload_form { print h1('Upload Excel File.'), start_multipart_form(), "Excel file:", filefield( -name=>'excel' ), br, submit( -name=>"Convert" ), end_form; } ## show the conversion process sub show_convert { print header, start_html ("Converting..."), "Converting...<br />", $filename, br, convert(), "<br />Done!"; } sub convert { ## convert to the xSV file... my $fh = upload('excel'); my $bytes_read = -s $fh; open OUTFILE, '>', $filename or die "Couldn't open output file: $!\n"; while ( my $bytes = read($fh, my $buffer, 2097152)) { print OUTFILE $buffer; } close OUTFILE; ## convert to xSV... txt_to_xSV(); ## convert xSV to XML... xSV_to_XML(); ## copy/back up file, not added yet "Received $bytes_read bytes<br />"; } sub txt_to_xSV { require Spreadsheet::ParseExcel; my $oExcel = new Spreadsheet::ParseExcel; my $dir = $ARGV[1]; chomp $dir; unless (-d $dir) { print "Can't find directory $dir"; exit; } my $filename = "$dir/$ARGV[0]"; chomp $filename; unless (-e $filename) { print "Can't find file $filename"; exit ; } my $fullfilename = "$filename.txt"; ## start work #1.1 Normal Excel97 open E2T, ">", $fullfilename or die $!; my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($filename); my($iR, $iC, $oWkS, $oWkC); foreach my $oWkS (@{$oBook->{Worksheet}}) { print "--------- SHEET:", $oWkS->{Name}, "\n"; print E2T $oWkS->{Name}, "|"; next unless defined $oWkS->{MinRow} and defined $oWkS->{MaxRow}; for my $iR ($oWkS->{MinRow} .. $oWkS->{MaxRow}) { for my $iC ($oWkS->{MinCol} .. $oWkS->{MaxCol}) { $oWkC = $oWkS->{Cells}[$iR][$iC]; next if ! defined $oWkC; print "( $iR , $iC ) =>", $oWkC->Value, "\n" if($oWkC); print E2T $oWkC->Value, "|"; } } print E2T "\n"; } close E2T; } sub xSV_to_XML { my $IN = "master.xls.txt"; my $OUT = "master.xml"; open (OUT, ">$OUT"); open(IN) or die ("Cannot open file ($IN)"); print OUT <<TOP; <?xml version="1.0" encoding="ISO-8859-1"?> TOP my $id = 0; my @columns = ("category","code","title","summary","prereq","group","subgroup","sequ +ence","rolemandatory","rolerecommended","roleoptional","url","modalit +y","length"); foreach my $row (<IN>){ $row =~ s#\s+$##; $row =~ s#&+#&amp;#g; $row =~ s#(?<=\|)\|#0|#g; my ($category,$code,$title,$summary,$prereq,$group,$subgroup,$sequence,$r +olemandatory,$rolerecommended,$roleoptional,$url,$modality,$length) = split ('\|', $row); #will ignore lank next if $group eq ""; foreach $_ (@columns){ xmlrow($_); } print OUT "</star:course>\n"; $id++; } print OUT "</star:learning-paths>\n"; } sub xmlrow{ my $colname = shift(@_); my $ostar="<star:"; my $estar="</star:"; print OUT "<star:$colname>${$colname}</star:$colname>\n"; } init();

In reply to CGI valid header error by csuhockey3

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found