X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FCookbook.pod;h=aa38da5c526761a9390d544596d0dac235eceef3;hb=47ae6960753acadac1da528c87c5d5009b675281;hp=2c126d17d12a606b8d0f0a6245189d146dda3767;hpb=5c0ff1281ecab26214c9b8279b8fb284385565f3;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Manual/Cookbook.pod b/lib/Catalyst/Manual/Cookbook.pod index 2c126d1..aa38da5 100644 --- a/lib/Catalyst/Manual/Cookbook.pod +++ b/lib/Catalyst/Manual/Cookbook.pod @@ -87,19 +87,20 @@ Catalyst Controller module 'upload' action: sub upload : Global { my ($self, $c) = @_; - if ($c->req->parameters->{form_submit} eq 'yes') { - my $upload = $c->req->upload('my_file'); - if ($upload->filename) { + + if ( $c->request->parameters->{form_submit} eq 'yes' ) { + + if ( my $upload = $c->request->upload('my_file') ) { + my $filename = $upload->filename; - my $fh = $upload->fh; - open(NEW_FILE, ">/tmp/upload/$filename") or die - "Can't open file for writing: $!"; - while ($fh->read(my $buf, 32768)) { - print NEW_FILE $buf; + my $target = "/tmp/upload/$filename"; + + unless ( $upload->link($target) || $upload->copy($target) ) { + die( "Failed to copy '$filename' to '$target': $!" ); } - close(NEW_FILE); } } + $c->stash->{template} = 'file_upload.html'; } @@ -122,21 +123,20 @@ Controller: sub upload : Local { my ($self, $c) = @_; - if ($c->req->parameters->{form_submit} eq 'yes') { - for my $field ($c->req->upload) { - my $upload = $c->req->upload($field); - if ($upload->filename) { - my $filename = $upload->filename; - my $fh = $upload->fh; - open(NEW_FILE, ">/tmp/upload/$filename") or die - "Can't open file for writing: $!"; - while ($fh->read(my $buf, 32768)) { - print NEW_FILE $buf; - } - close(NEW_FILE); + + if ( $c->request->parameters->{form_submit} eq 'yes' ) { + + for my $field ( $c->req->upload ) { + + my $filename = $upload->filename; + my $target = "/tmp/upload/$filename"; + + unless ( $upload->link($target) || $upload->copy($target) ) { + die( "Failed to copy '$filename' to '$target': $!" ); } } } + $c->stash->{template} = 'file_upload.html'; }