file upload
am 05.01.2006 16:00:02 von kalyan kamesh
Hi
I'm using the following code for PDF file upload via perl
The code is working.
but the file size is zero bytes
any suggestions
#!/usr/local/bin/perl -w
####### Load Needed Perl Modules ######
use strict;
# Make HTML/FORMS/UPLOADING easy to deal with
use CGI;
# Report errors in the browser
use CGI::Carp 'fatalsToBrowser';
# Limit file size
$CGI::POST_MAX=500000; # max 100K posts
####### End Perl Module Load #######
# Create new CGI object
my $q = new CGI;
if ( $q->param() ) {
# read filehandle from param and set to binary mode
my $filehandle = $q->param('proof_file');
binmode($filehandle);
# Strip off WINDOZE path crap
$_=$filehandle;
s/.*\\//;
my $filename=$_;
# open file for output - change this to suit your needs!!!
open(OUT,">upload/$filename") || die $!;
binmode(OUT);
# process $filehandle
{
my $buffer;
while ( read($filehandle,$buffer,500000) ) {
print OUT $buffer;
}
}
# close output file
close(OUT);
# show success
print $q->header,
$q->start_html,
$q->p('File uploaded: $filename'),
$q->end_html;
exit(0);
}
Thanks & Regards
kalyan kamesh
Re: file upload
am 05.01.2006 18:38:36 von Gunnar Hjalmarsson
kalikoi@gmail.com wrote:
> I'm using the following code for PDF file upload via perl
>
> The code is working.
>
> but the file size is zero bytes
>
> any suggestions
Use the CPAN module CGI::UploadEasy. Among other things, it checks for
common mistakes with respect to file uploads.
#!/usr/local/bin/perl -T
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI::UploadEasy;
my $uploaddir = '/path/to/upload/directory';
my $ue = CGI::UploadEasy->new(-uploaddir => $uploaddir);
my $cgi = $ue->cgiobject;
my $info = $ue->fileinfo;
print $cgi->header, $cgi->start_html,
$cgi->p('Files uploaded: ', keys %$info),
$cgi->end_html;
__END__
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: file upload
am 05.01.2006 19:56:42 von Paul Lalli
kalikoi@gmail.com wrote:
> I'm using the following code for PDF file upload via perl
>
> The code is working.
> but the file size is zero bytes
You have an odd definition of "working".
> # Strip off WINDOZE path crap
> $_=$filehandle;
> s/.*\\//;
> my $filename=$_;
Yuck. perldoc File::Basename
> # process $filehandle
> {
> my $buffer;
> while ( read($filehandle,$buffer,500000) ) {
> print OUT $buffer;
> }
> }
How was the parameter which lead to $filehandle generated? In your
form, did you remember to add the enctype="multipart/form-data"
attribute to the form tag? Are you sure the input was type="filefield"
rather than type="text"? Show us the whole process, not just one side
of it.
Paul Lalli
Re: file upload
am 05.01.2006 19:56:47 von Jim Gibson
In article <1136473202.221631.182480@g49g2000cwa.googlegroups.com>,
wrote:
> Hi
>
>
> I'm using the following code for PDF file upload via perl
>
>
> The code is working.
>
>
> but the file size is zero bytes
>
>
> any suggestions
I answered the same question that you posted on comp.lang.perl.misc.
Please do not multi-post.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: file upload
am 05.01.2006 20:05:14 von Matt Garrish
"Paul Lalli" wrote in message
news:1136487402.527007.99190@g49g2000cwa.googlegroups.com...
> kalikoi@gmail.com wrote:
>> I'm using the following code for PDF file upload via perl
>>
>> The code is working.
>> but the file size is zero bytes
>
>
> How was the parameter which lead to $filehandle generated? In your
> form, did you remember to add the enctype="multipart/form-data"
> attribute to the form tag? Are you sure the input was type="filefield"
I suspect you meant: type="file"
Matt
Re: file upload
am 05.01.2006 20:16:03 von Gunnar Hjalmarsson
Paul Lalli wrote:
> kalikoi@gmail.com wrote:
>>
>> # Strip off WINDOZE path crap
>> $_=$filehandle;
>> s/.*\\//;
>> my $filename=$_;
>
> Yuck. perldoc File::Basename
Nope. File::Basename wouldn't know from which platform the file was
sent, would it?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: file upload
am 05.01.2006 20:24:53 von Paul Lalli
Matt Garrish wrote:
> "Paul Lalli" wrote in message
> > Are you sure the input was type="filefield"
>
> I suspect you meant: type="file"
Whoops. Yes, I was confusing the HTML attribute with the CGI.pm
shortcut that generates that HTML. Thanks for catching that.
Paul Lalli
Re: file upload
am 05.01.2006 20:31:02 von Paul Lalli
Gunnar Hjalmarsson wrote:
> Paul Lalli wrote:
> > kalikoi@gmail.com wrote:
> >>
> >> # Strip off WINDOZE path crap
> >> $_=$filehandle;
> >> s/.*\\//;
> >> my $filename=$_;
> >
> > Yuck. perldoc File::Basename
>
> Nope. File::Basename wouldn't know from which platform the file was
> sent, would it?
Nobody *knows*. But the author of this code is clearly assuming it was
MSWin32...
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
fileparse_set_fstype('MSWin32');
print basename('\\foo\\bar\\baz.pm');
__END__
baz.pm
Paul Lalli
Re: file upload
am 05.01.2006 21:11:46 von Gunnar Hjalmarsson
Paul Lalli wrote:
> Gunnar Hjalmarsson wrote:
>>Paul Lalli wrote:
>>>kalikoi@gmail.com wrote:
>>>>
>>>> # Strip off WINDOZE path crap
>>>> $_=$filehandle;
>>>> s/.*\\//;
>>>> my $filename=$_;
>>>
>>>Yuck. perldoc File::Basename
>>
>>Nope. File::Basename wouldn't know from which platform the file was
>>sent, would it?
>
> Nobody *knows*. But the author of this code is clearly assuming it was
> MSWin32...
Well, yes, which may well be a not-very-wise assumption. Generally, when
designing a file upload request, File::Basename don't help you with that.
Please feel free to criticize the regex I'm using for the purpose in
CGI::UploadEasy:
s#.*[\]:\\/]##;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: file upload
am 05.01.2006 21:15:28 von Gunnar Hjalmarsson
Gunnar Hjalmarsson wrote:
> ... when designing a file upload request, File::Basename don't ...
s/request/app/;
s/don't/doesn't/;
Sorry.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: file upload
am 05.01.2006 21:20:37 von Paul Lalli
Gunnar Hjalmarsson wrote:
> Paul Lalli wrote:
> > Nobody *knows*. But the author of this code is clearly assuming it was
> > MSWin32...
>
> Well, yes, which may well be a not-very-wise assumption. Generally, when
> designing a file upload request, File::Basename don't help you with that.
I agree.
> Please feel free to criticize the regex I'm using for the purpose in
> CGI::UploadEasy:
>
> s#.*[\]:\\/]##;
Well, obviously it would cause problems for any filename that did
happen to contain one of those characters. But really, if a user
creates a file with those characters, they deserve what they have
coming.
Out of curiousity, what type of system uses the ] character as a path
separator?
Paul Lalli
Re: file upload
am 05.01.2006 23:58:32 von Gunnar Hjalmarsson
Paul Lalli wrote:
> Gunnar Hjalmarsson wrote:
>>Please feel free to criticize the regex I'm using for the purpose in
>>CGI::UploadEasy:
>>
>> s#.*[\]:\\/]##;
>
> Well, obviously it would cause problems for any filename that did
> happen to contain one of those characters. But really, if a user
> creates a file with those characters, they deserve what they have
> coming.
Indeed. And in the case of file uploads, it's not critical. A failure to
correctly extract the actual filename just results in the uploaded file
be named slightly different compared to the original file.
> Out of curiousity, what type of system uses the ] character as a path
> separator?
According to the File::Basename POD: VMS. I have never seen it IRL,
which is one reason why I asked. :)
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: file upload
am 06.01.2006 01:24:52 von Jim Gibson
In article <425mgvF1hglfvU1@individual.net>, Gunnar Hjalmarsson
wrote:
> Paul Lalli wrote:
> > Gunnar Hjalmarsson wrote:
>
> > Out of curiousity, what type of system uses the ] character as a path
> > separator?
>
> According to the File::Basename POD: VMS. I have never seen it IRL,
> which is one reason why I asked. :)
As I recall, it is a directory termination character, actually, and '.'
is the path separator. For example, a full path name might be:
[dira.dirb.dirc]filename.typ
(it's been many, many years since I worked on VMS, however).
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: file upload
am 06.01.2006 01:51:34 von Gunnar Hjalmarsson
Jim Gibson wrote:
> Gunnar Hjalmarsson wrote:
>> Paul Lalli wrote:
>>> Gunnar Hjalmarsson wrote:
>>>>
>>>> s#.*[\]:\\/]##;
>>>
>>> Out of curiousity, what type of system uses the ] character as a path
>>> separator?
>>
>> According to the File::Basename POD: VMS. I have never seen it IRL,
>> which is one reason why I asked. :)
>
> As I recall, it is a directory termination character, actually, and '.'
> is the path separator. For example, a full path name might be:
>
> [dira.dirb.dirc]filename.typ
Thanks for the correction. The above s/// expression still ought to
serve the purpose of extracting the actual file name, right?
> (it's been many, many years since I worked on VMS, however).
Disclaimer noted. ;-)
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: file upload
am 06.01.2006 08:47:11 von martin
Jim Gibson wrote:
> As I recall, it is a directory termination character, actually, and '.'
> is the path separator. For example, a full path name might be:
>
> [dira.dirb.dirc]filename.typ
In fact, a complete VMS filename looks like
node"login info"::device:[path.to.file]filename.type;version
For each of the fields, there's some default (e.g. node"login info"::
defaults to local node with no login info needed, the default for
the device is the current device, etc.).
VMS' C RTL (and thus Perl) understand Unix notation (with the leading
path element being the device).
cu,
Martin
--
| Martin Vorlaender | OpenVMS rules!
Microsoft's answer | work: mv@pdv-systeme.de
to OpenVMS is | http://www.pdv-systeme.de/users/martinv/
Windows NT 10.0. | home: martin@radiogaga.harz.de