I am not a regular Perl user so I cut and paste to get what I need done and uploading a file is the most advanced thing I have tried to do. There is already a web page on my site that uploads files so I have put together what I need and do not understand why it doesn't work. Another 5 hours of banging my head won't help any more than the first 5 hours. Below is the essentials of what I am doing. The file upload.cgi puts out the form that calls upload_proc.cgi. upload_proc.cgi includes a package util.pm containing a subroutine get_file to do the actual upload. I don't touch util.pm and it's subroutine get_file is used by other programs (the ones I cut and pasted from). I have two identical files one called filename and the other called filename.csv. I select the file with no extension, click the Upload button, and get an error from the get_file subroutine - CGI nodata(C:\temp\filename).
#this is a stripped down version of the code #no colors, no fonts, no extra input buttons #--------------------------------------- #this is part of <upload.cgi> which outputs the #the form and makes the call to upload_proc.cgi #when the Upload button is clicked #--------------------------------------- <form action="upload_proc.cgi" method="POST" enctype="multipart/form-d +ata"> <center> <table border = 5 cellpadding="5"> <tr><th>Data Upload</th></tr> <tr><td><input type=file name=file_data accept=\"*\"></td></tr> <tr><td><input type=submit name=submit value="Upload">&nbsp;&nbsp; +==&gt;&gt;</td></tr> </table> </center> </form> #--------------------------------------- #end of partial upload.cgi #--------------------------------------- #--------------------------------------- #this is the stripped down <upload_proc.cgi> #--------------------------------------- #!/usr/bin/perl use utf8; use encoding "utf-8"; use CGI; use Bos::util qw($dbh); use Text::CSV; use strict; Bos::util::db_connect(); my $user = Bos::util::authenticate("COOKIE","COOKIE"); Bos::util::auth_error($user); if ($user->{'Type'} ne "B") { Bos::util::user_error("<center>Access denied.<br>Your account is n +ot authorized for this function.</center>"); } my $query; my $sth; my $myrow; my $cgi = Bos::util::cgi_query(); if( $cgi->param('submit') eq "Upload" ) { Bos::util::send_header("New Data Results", ""); chdir( "/ccshome/www/customer_data_upload" ) or die; #permissions + are 1777 on this folder my $new_file_data = upload_file($cgi->param('file_data')); } Bos::util::send_footer; #--------------------------------------- #this is the <sub upload_file> that is in upload_proc.cgi #--------------------------------------- sub upload_file { my $filename = shift; return Bos::util::get_file($filename); } #--------------------------------------- #end of upload_proc.cgi #--------------------------------------- #--------------------------------------- #this is the <sub Bos::util::get_file> #extracted from the Bos::util.pm #used by upload_proc.cgi #--------------------------------------- sub get_file { my $filename = shift; my $query = cgi_query(); return 0 unless defined $filename; my $fh = $query->upload( $filename ); if( $fh ) { rename("${filename}.csv", "${filename}.old.csv"); # open for writing open(DATA,">${filename}.csv") or die "Can't create ${filename} +.csv ($!)\n"; while( <$fh> ) { print DATA; # write the file out } close(DATA); return 1; # successful return # there must be a CGI error } else { my ($errstr) = $query->cgi_error; if ($errstr) { print "CGI error($filename): $errstr<br>\n"; } else { print "CGI nodata($filename)<br>\n"; } return 0; } }

In reply to Uploading a file by rightfield

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.