newbie: file uploads not working :(

2002-09-12 Thread Alan

Hi folks.  I'm new to the list, and relatively new to mod_perl, but a
big project thrown my way put me right in the middle, and I think I've
fared well so far, with one exception... file uploads.  I've based my
code off the apache::request documentation and the file upload code
snippet posted here and on the perl.apache.org site.

I have the following configured:



SetHandler perl-script
PerlHandler Test::Upload
PerlInitHandler Apache::StatINC


My Test/Upload.pm looks something like this:

-
package Test::Upload;
use strict;
use Apache::Constants qw(:common);
use Apache::Log;
use Apache::File();
use Apache::Request;

sub handler {
   my $r = shift;

   my $form = form();

   if (my $file = $form->{UPLOAD})
   {
  my $filelocation="/home/alan/code/test/htdocs/files";
  my $filename = $file->filename; # If you need the name
  open F, ">$filelocation";
  my $filehandle = $file->fh;
  while (my $d = <$filehandle>) 
  {
 print F $d;
  }
  close F;
   }

   $r->content_type('text/html');
   $r->send_http_header;
   print <

Upload a file:  

END

   return OK;
}

1;

# This is a copy of the form code from the snippets page
sub form {
   use Apache::Request;
   my $r = Apache->request();
   my $apr = Apache::Request->new($r, DISABLE_UPLOADS => 1);
   my @keys = $apr->param;

   my %form;
   foreach my $key(@keys) {

  my @value = $apr->param($key);
  next unless scalar @value;

  if ( @value > 1 ) {
 $form{$key} = \@value;
  } else {
 $form{$key} = $value[0];
  }
   }

   my $upload = $apr->upload;
   if ($upload) {
  $form{UPLOAD} = $upload;
   }

   return \%form;
}

-

As far as I can tell, the code should see the upload, grab the upload
via $apr->upload, and use it.  However, nothing happens. 

Even if I put in copious debug (for example, code that prints out when
if($upload){ } is hit), nothing.  It's as if the upload is not taking
place at all.

Can anyone give me a hand with this, I'm literally beating my head
against the table!

TIA

Alan

-- 
Alan "Arcterex" <[EMAIL PROTECTED]>   -=][=-   http://arcterex.net
"I used to herd dairy cows. Now I herd lusers. Apart from the isolation, I
think I preferred the cows. They were better conversation, easier to milk, and
if they annoyed me enough, I could shoot them and eat them." -Rodger Donaldson



Re: newbie: file uploads not working :(

2002-09-12 Thread Alan

(replying to self in true newbie style)

On Thu, Sep 12, 2002 at 12:08:11PM -0700, Alan wrote:
> # This is a copy of the form code from the snippets page
> sub form {
>use Apache::Request;
>my $r = Apache->request();
>my $apr = Apache::Request->new($r, DISABLE_UPLOADS => 1);

The DISABLE_UPLOADS wasn't actually there, that was a holdover from
testing to try to get *any* response from an upload that I forgot about
when I pasted the code in.  Even if it was in there, it should have
errored out if an upload was attempted right?

alan the sheepish



-- 
Alan "Arcterex" <[EMAIL PROTECTED]>   -=][=-   http://arcterex.net
"I used to herd dairy cows. Now I herd lusers. Apart from the isolation, I
think I preferred the cows. They were better conversation, easier to milk, and
if they annoyed me enough, I could shoot them and eat them." -Rodger Donaldson



Re: newbie: file uploads not working :(

2002-09-12 Thread Geoffrey Young



[snip]
> 
> # This is a copy of the form code from the snippets page
> sub form {
>use Apache::Request;
>my $r = Apache->request();
>my $apr = Apache::Request->new($r, DISABLE_UPLOADS => 1);

I think DISABLE_UPLOADS should be 0, not 1.  I can't find any other 
obvious errors in what you have...

if that doesn't work out, you may want to try this code

http://www.modperlcookbook.org/code/ch03/Cookbook/PrintUploads.pm

and see if it helps clarify things.  in particular, you might want to 
add a call to $apr->parse to see what it returns - that may help you 
debug things some more.

you may want to take a look and the tempname() and link() methods for 
your uploaded object once you get things figured out :)


HTH

--Geoff




Re: newbie: file uploads not working :(

2002-09-12 Thread Alan

On Thu, Sep 12, 2002 at 03:24:50PM -0400, Geoffrey Young wrote:
> 
> 
> [snip]
> >
> ># This is a copy of the form code from the snippets page
> >sub form {
> >   use Apache::Request;
> >   my $r = Apache->request();
> >   my $apr = Apache::Request->new($r, DISABLE_UPLOADS => 1);
> 
> I think DISABLE_UPLOADS should be 0, not 1.  I can't find any other 
> obvious errors in what you have...

Yea, caught this just after I sent the email :)

> and see if it helps clarify things.  in particular, you might want to 
> add a call to $apr->parse to see what it returns - that may help you 
> debug things some more.

It returns 0.  I threw the example code from the apache::request parse()
section into sub form and nada.  I'm wondering if it has somthing to do
with my HTML or something?  Is my html-fu doing something stupid?

Thanks

alan

-- 
Alan "Arcterex" <[EMAIL PROTECTED]>   -=][=-   http://arcterex.net
"I used to herd dairy cows. Now I herd lusers. Apart from the isolation, I
think I preferred the cows. They were better conversation, easier to milk, and
if they annoyed me enough, I could shoot them and eat them." -Rodger Donaldson



Re: newbie: file uploads not working :(

2002-09-12 Thread Paul de Repentigny

Alan,

Much easier to read would be:

sub handler {
my $r = Apache::Request->new(shift);
my $file_name = $r->param('see_below');
my $file = $r->upload('see_below');
if ($file) {
# here you go...
# filehandle is in $file->fh;
}
}

On the HTML side,  should read:


Note: If you ever use them in file posts, don't forget to clean the file
names, especially when it comes from Windows machine...

Paul.

Alan écrivit:

> Hi folks.  I'm new to the list, and relatively new to mod_perl, but a
> big project thrown my way put me right in the middle, and I think I've
> fared well so far, with one exception... file uploads.  I've based my
> code off the apache::request documentation and the file upload code
> snippet posted here and on the perl.apache.org site.
> 
> I have the following configured:
> 
> ...snip... 
> -





Re: newbie: file uploads not working :(

2002-09-12 Thread Geoffrey Young


> Note: If you ever use them in file posts, don't forget to clean the file
> names, especially when it comes from Windows machine...
> 

I've found this to be reasonably portable for getting just the 
filename (sans path) - YMMV

my ($name) = $upload->filename =~ m!([^/\\]*$)!;

--Geoff




Re: newbie: file uploads not working :(

2002-09-12 Thread darren chamberlain

* Geoffrey Young <[EMAIL PROTECTED]> [2002-09-12 15:45]:
> > Note: If you ever use them in file posts, don't forget to clean the
> > file names, especially when it comes from Windows machine...
> 
> I've found this to be reasonably portable for getting just the 
> filename (sans path) - YMMV
> 
> my ($name) = $upload->filename =~ m!([^/\\]*$)!;

  use File::Basename;
  my $name = basename($upload->filename);

(darren)

-- 
There is no expedient to which a man will not go to
avoid the real labour of thinking.



Re: newbie: file uploads not working :(

2002-09-12 Thread simran

In your HTML, do you have as part of the  an enctype defined... 

eg. 

  


If you do not have the enctype then your script will not be able to get
the file handle... 

simran.

On Fri, 2002-09-13 at 05:32, Alan wrote:
> On Thu, Sep 12, 2002 at 03:24:50PM -0400, Geoffrey Young wrote:
> > 
> > 
> > [snip]
> > >
> > ># This is a copy of the form code from the snippets page
> > >sub form {
> > >   use Apache::Request;
> > >   my $r = Apache->request();
> > >   my $apr = Apache::Request->new($r, DISABLE_UPLOADS => 1);
> > 
> > I think DISABLE_UPLOADS should be 0, not 1.  I can't find any other 
> > obvious errors in what you have...
> 
> Yea, caught this just after I sent the email :)
> 
> > and see if it helps clarify things.  in particular, you might want to 
> > add a call to $apr->parse to see what it returns - that may help you 
> > debug things some more.
> 
> It returns 0.  I threw the example code from the apache::request parse()
> section into sub form and nada.  I'm wondering if it has somthing to do
> with my HTML or something?  Is my html-fu doing something stupid?
> 
> Thanks
> 
> alan
> 
> -- 
> Alan "Arcterex" <[EMAIL PROTECTED]>   -=][=-   http://arcterex.net
> "I used to herd dairy cows. Now I herd lusers. Apart from the isolation, I
> think I preferred the cows. They were better conversation, easier to milk, and
> if they annoyed me enough, I could shoot them and eat them." -Rodger Donaldson
> 
> 
> 






Re: newbie: file uploads not working :(

2002-09-12 Thread Steve Piner



Geoffrey Young wrote:
> 
> > Note: If you ever use them in file posts, don't forget to clean the file
> > names, especially when it comes from Windows machine...
> >
> 
> I've found this to be reasonably portable for getting just the
> filename (sans path) - YMMV
> 
> my ($name) = $upload->filename =~ m!([^/\\]*$)!;

Erm.. portable maybe, (MacOS?) but what about secure? That lets through
shell meta-characters, which may or may not be a problem. (What if the
filename is '|mail%20cracker@somewhere'?)

I'm inclined to be a little more restrictive:

my ($name) = $upload->filename =~ m!([^\w\.]*$)!;

(And even then, that's not secure under windows - NUL, LPT1, etc.)

Steve

-- 
Steve Piner
Web Applications Developer
Marketview Limited
http://www.marketview.co.nz



Re: newbie: file uploads not working :(

2002-09-13 Thread Geoffrey Young



>>my ($name) = $upload->filename =~ m!([^/\\]*$)!;
> 
> 
>   use File::Basename;
>   my $name = basename($upload->filename);

much better :)

--Geoff