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

mod_perl upload woes

by drfrog (Deacon)
on Jan 12, 2004 at 20:36 UTC ( [id://320770]=perlquestion: print w/replies, xml ) Need Help??

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

Hello

Ive been trying to implement a mod_perl 1.0 on apache 1.3.28 based upload scenario for a while now, as soon as i try to add other parameters to the form it gets funky, either a problem exists with modperl and switching get/post and/or it dies upon trying to load in $apr->upload. I am basically trying to just pass a variable to save the file to

here are a few links ive found info at
File_Upload_with_Apache__Request
mod_perl cookbook example

when i add into the second links code something like
%args=$r->args; my $saveto=$args{saveto};

it fails to either pass the variable or load $apr->upload properly

can anyone point to a way to do this? or should i just use CGI::Upload or some other solution?

thanks!

Replies are listed 'Best First'.
Re: mod_perl upload woes
by jeffa (Bishop) on Jan 12, 2004 at 22:13 UTC

    I too have tried to successfully implement robust file uploading with mod_perl. I failed.

    This is why it's still handy to have a cgi-bin directory, which i what i use to solve this problem - a CGI script. Something like the following should get you started. I store the files in another directory outside of the cgi-bin dir, say UPLOAD (caps for emphasis only).

    use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use File::Basename; my $PATH = '/path/to/upload/not/in/cgi-bin/UPLOAD'; print header, start_html('not very safe uploader'); if (param('go')) { my $handle = upload('the_file'); my $name = param('the_file'); open OUT, '>', "$PATH/$name" or die "can't open target $name: $!"; print OUT while <$handle>; } my @file = map basename($_), <$PATH/*>; print start_multipart_form(), p('upload:', filefield('the_file',undef,50,80)), p(submit('go')), end_form, hr, ul(li[ map a({href=>"/UPLOAD/$_"},$_), @file ]), # YMMV here hr, end_html, ;
    However, i do hope that someone can ellaborate more on why mod_perl has difficulty with file uploads.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: mod_perl upload woes
by edoc (Chaplain) on Jan 12, 2004 at 23:21 UTC

    I've implemented uploads in mod_perl with no apparent dramas.. The first time (a couple years back) it took me a while to get my head around it, but I tackled it the other day again and was chuffed at how easy it was.

    What errors/symptoms are you guys seeing?

    when i add into the second links code something like
    %args=$r->args;
    my $saveto=$args{saveto};

    Have you tried:

    my $saveto = $r->param('saveto');

    cheers,

    J

      hmm i dont know what kind of problems i was having earlier, but now coffee seems to have helped!!

      i think part of this might have to do with getting parameter in these two ways, which just made sense when edoc showed me the line:
      #normal $r from my POV sub handler { my $r=Apache::request(); %args=$r->args; my $sitename=$args{sitename}; }
      or
      #$apr sub handler { my $r=Apache::request(); my $apr = Apache::Request->new( $r , POST_MAX => 10 * 1024 * 1024, # in byte +s, so 10M DISABLE_UPLOADS => 0); my $sitename = $apr->param('sitename'); }
      it seems obvious that $apr loads parameters in differently ? or am i mistaken/used to Apache::request as opposed to Apache::Request? here is the working code

      package PP::Uploads; use Apache::Constants qw(OK); use Apache::Request; use Apache::Util qw(escape_html); use strict; sub handler { # Standard stuff, with added options... my $r = Apache::Request->new(shift, POST_MAX => 10 * 1024 * 1024, # in byte +s, so 10M DISABLE_UPLOADS => 0); my $status = $r->parse(); my $sitename = $r->param('sitename'); # Return an error if we have problems. return $status unless $status == OK; $r->send_http_header('text/html'); $r->print("<html><body>\n"); $r->print("<h1>Upload files</h1>"); # Iterate through each uploaded file. foreach my $upload ($r->upload) { my $filename = $upload->filename; my $filehandle = $upload->fh; my $size = $upload->size; $r->print("You sent me a file named $filename, $size bytes<br>"); $r->print("The first line of the file is: <br>"); my $dir="/tmp/$sitename/"; my $line; while ( <$filehandle>) { $line.=$_; } mkdir $dir; my $image=$dir.$filename; open FH ,">$image" or die $!; print FH $line; close FH; # $r->print(escape_html($line), "<br>"); } $r->print("Done......<br>"); # Output a simple form. $r->print(<<EOF); <form enctype="multipart/form-data" name="files" action="/PP/upload" + method="POST"> <input type=hidden name=sitename value=$sitename> File 1 <input type="file" name="file1"><br> File 2 <input type="file" name="file2"><br> File 3 <input type="file" name="file3"><br><br> <input type="submit" name="submit" value="Upload these files"> </form> </body></html> EOF return OK; }; 1;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2024-04-24 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found