uploadInfo() woes...

uploadInfo() woes...

am 11.07.2006 06:47:16 von matt

So, it has been a long, long time since I've posted to this group, and
about as long since I've programmed anything. (ca. 2000 C.E.) I have
become mildly retarded, rusty, fatter, etc., so bear with me.

I was recently roped into building the back end for a web site which
must include a feature in which members can upload files that will be
linked to from their personal pages.

sub uploadFile() {
my $fh = upload('file');
if (!$fh) { return; }
my $type = uploadInfo(param('file'), 'content-type');
die $type;

In this bit of code, I get to the die statement, which tells me that I
am getting a file handle from upload(). (So does 'die $fn;', which
I've tested.) $type, however, is consistently undef, and I cannot
figure out why.

The object is to organize these files that users may be uploading by
type, without trusting file extensions to be accurate, which is why I
was relying on Content-Type. I don't know if this is a problem of the
browser not sending the information (although I had identical results
using Safari and Firefox), or a problem with my code, or what.

Either way, I'm pretty much sitting here scratching my head and poking
at the screen like a monkey, so if someone can help me sort this out so
I can move on with my life of not programming, I would really
appreciate it. A few more dead ends and I swear I'll fling poo.

Matt

Re: uploadInfo() woes...

am 11.07.2006 08:21:29 von Gunnar Hjalmarsson

matt wrote:
>
> my $type = uploadInfo(param('file'), 'content-type');

my $type = uploadInfo($fh)->{'Content-Type'};

> The object is to organize these files that users may be uploading by
> type, without trusting file extensions to be accurate,

In that case you cannot make use of the content-type according to
uploadInfo(). You rather need to have the uploaded file examined by an
appropriate module such as the CPAN module File::MMagic.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: uploadInfo() woes...

am 11.07.2006 09:57:44 von matt

Gunnar Hjalmarsson wrote:

> In that case you cannot make use of the content-type according to
> uploadInfo(). You rather need to have the uploaded file examined by an
> appropriate module such as the CPAN module File::MMagic.

So far as I can tell, this is a slick module. I made up a little test
script and checked all the filetypes on my Desktop :)

Anyway, I'm having some trouble implementing it with the CGI.pm
upload() function.

According to the CGI.pm docs:

"When called with the name of an upload field, upload() returns a
filehandle, or undef if the parameter is not a valid filehandle."

Theoretically then, shouldn't this work?

my $fh = upload('file');
if (!$fh) { return; }
my $mm = new File::MMagic;
my $type = $mm->checktype_filehandle($fh);

or is the filehandle returned by upload different than Filehandle::new?
The above code yeilds this error message:

Undefined subroutine Fh::seek
at /Library/Perl/5.8.6/File/MMagic.pm line 802

I guess what I'm getting at is wondering if there's a way to check
filetype before writing the uploaded file to a directory. For example,
if the uploaded file is supposed to be an image, I'd like to check for
image/jpeg or image/gif before I write the file to the directory.

Thanks for the help,
Matt

Re: uploadInfo() woes...

am 11.07.2006 15:14:12 von Gunnar Hjalmarsson

matt wrote:
> According to the CGI.pm docs:
>
> "When called with the name of an upload field, upload() returns a
> filehandle, or undef if the parameter is not a valid filehandle."
>
> Theoretically then, shouldn't this work?
>
> my $fh = upload('file');
> if (!$fh) { return; }
> my $mm = new File::MMagic;
> my $type = $mm->checktype_filehandle($fh);
>
> or is the filehandle returned by upload different than Filehandle::new?
> The above code yeilds this error message:
>
> Undefined subroutine Fh::seek
> at /Library/Perl/5.8.6/File/MMagic.pm line 802
>
> I guess what I'm getting at is wondering if there's a way to check
> filetype before writing the uploaded file to a directory.

I don't know the reason for that error, but you can also try the
CGI::Upload module. It somehow integrates CGI.pm and File::MMagic.

Otherwise you can always complete the upload first, and then, if the
file type check isn't satisfactory, simply remove the file.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl