Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload a file using HTML and Perl
    text
    copied!<p>I am trying to do a simple upload of a .csv file and save it on my server. I'm not an expert in HTML or Perl, but due to client constraints this is what I must do.</p> <p>Here is the HTML:</p> <pre><code>&lt;form action="/path/to/service" target="_self" method="POST" enctype="multipart/form-data"&gt; File: &lt;input type="file" name="attachment" size="50"&gt; &lt;SUBMIT_RESET Upload File&gt; &lt;/form&gt; </code></pre> <p>The Perl code looks like this:</p> <pre><code> my $sql = "SELECT NOW()"; my $date = $DB-&gt;get_field($sql); my ($path, $ext) = split(/\./, $in{'attachment'}); my @pathParts = split(/\//, $path); my $filename = $pathParts[@pathParts - 1] . " - " . $date; if ($ext eq "csv") { open (OUTFILE, "&gt;", "$datadir/imports/" . $filename . "." . $ext); while (read($in{'attachment'}, $buffer, 1024)) { $file .= $buffer; } print OUTPUT $file; close (OUTFILE); } </code></pre> <p>Can anyone please give me some direction as to what I'm doing wrong. I get the file located at the correct path, but it's always empty.</p> <p>Now, the code base I'm dealing with here is horrible and I cannot use strict.</p> <p>Suggestions?</p> <p>EDIT1: To attempt to answer how $in{'attachment'} is populated I've included a snippet of code of how forms are handled.</p> <p>if ($ENV{'CONTENT_TYPE'} =~ m#^multipart/form-data#) {<br> my $cgi = new CGI; my @names = $cgi->param;</p> <pre><code> foreach $name (@names) { my @value = $cgi-&gt;param($name); if (scalar(@value) &gt; 1) { foreach $val (@value) { $in{$name} .= $val."\0"; } $in{$name} =~s/\\0$//; } else { my $value = $cgi-&gt;param($name); #my $value = join "\0", @value; $in{$name} = $value; } #my $value = $cgi-&gt;param($name); #my $value = join "\0", @value; #$in{$name} = $value; } </code></pre> <p>EDIT2:</p> <p>To summarize the solution provided by ikegami...</p> <p>I missed that the file handles were different for the read and print statements - <code>OUTFILE</code> and <code>OUTPUT</code>. Clearly that is completely a novice mistake and for that I apologize!</p> <p>Best wishes to all who tried to help me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload