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

Uploading a file

by rightfield (Sexton)
on May 17, 2009 at 14:57 UTC ( #764516=perlquestion: print w/replies, xml ) Need Help??

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

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 containing a subroutine get_file to do the actual upload. I don't touch 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 #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; } }

Replies are listed 'Best First'.
Re: Uploading a file
by Corion (Patriarch) on May 17, 2009 at 15:04 UTC
    ... upload_file($cgi->param('file_data')); ... my $fh = $query->upload( $filename );

    You're not properly using CGI::upload(). upload() takes the parameter name, not the parameter value. So, by changing it into the following, it could start to work:

    my $fh = $query->upload( 'file_data' );

    But then again, you could maybe work some more on your code or error messages.

      Thank you, thank you, thank you, and thank you once again!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://764516]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2023-01-28 03:28 GMT
Find Nodes?
    Voting Booth?

    No recent polls found