Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

file upload

2 views
Skip to first unread message

kal...@gmail.com

unread,
Jan 5, 2006, 10:00:02 AM1/5/06
to
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

Gunnar Hjalmarsson

unread,
Jan 5, 2006, 12:38:36 PM1/5/06
to
kal...@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

Jim Gibson

unread,
Jan 5, 2006, 1:56:47 PM1/5/06
to
In article <1136473202.2...@g49g2000cwa.googlegroups.com>,
<kal...@gmail.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

Paul Lalli

unread,
Jan 5, 2006, 1:56:42 PM1/5/06
to
kal...@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

Gunnar Hjalmarsson

unread,
Jan 5, 2006, 2:16:03 PM1/5/06
to
Paul Lalli wrote:

> kal...@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?

Matt Garrish

unread,
Jan 5, 2006, 2:05:14 PM1/5/06
to

"Paul Lalli" <mri...@gmail.com> wrote in message
news:1136487402....@g49g2000cwa.googlegroups.com...

> kal...@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


Paul Lalli

unread,
Jan 5, 2006, 2:24:53 PM1/5/06
to
Matt Garrish wrote:
> "Paul Lalli" <mri...@gmail.com> 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

Paul Lalli

unread,
Jan 5, 2006, 2:31:02 PM1/5/06
to
Gunnar Hjalmarsson wrote:
> Paul Lalli wrote:
> > kal...@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

Gunnar Hjalmarsson

unread,
Jan 5, 2006, 3:11:46 PM1/5/06
to
Paul Lalli wrote:
> Gunnar Hjalmarsson wrote:
>>Paul Lalli wrote:
>>>kal...@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

unread,
Jan 5, 2006, 3:15:28 PM1/5/06
to
Gunnar Hjalmarsson wrote:
> ... when designing a file upload request, File::Basename don't ...

s/request/app/;
s/don't/doesn't/;

Sorry.

Paul Lalli

unread,
Jan 5, 2006, 3:20:37 PM1/5/06
to
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

Gunnar Hjalmarsson

unread,
Jan 5, 2006, 5:58:32 PM1/5/06
to
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. :)

Jim Gibson

unread,
Jan 5, 2006, 7:24:52 PM1/5/06
to
In article <425mgvF...@individual.net>, Gunnar Hjalmarsson
<nor...@gunnar.cc> 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

(it's been many, many years since I worked on VMS, however).

Gunnar Hjalmarsson

unread,
Jan 5, 2006, 7:51:34 PM1/5/06
to
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. ;-)

Martin Vorlaender

unread,
Jan 6, 2006, 2:47:11 AM1/6/06
to
Jim Gibson <jgi...@mail.arc.nasa.gov> 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: m...@pdv-systeme.de
to OpenVMS is | http://www.pdv-systeme.de/users/martinv/
Windows NT 10.0. | home: mar...@radiogaga.harz.de

0 new messages