Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • 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? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2023-01-28 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?