Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Problem with uploaded (http) files with File::MMagic

by Anonymous Monk
on Dec 22, 2012 at 15:15 UTC ( [id://1010022]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I will try to explain this as clearly as I can. I would appreciate your help very much.

I am using File::MMagic module to determine if a particular file is an mpeg file. I have tried this on 2 separate hosted linux systems.

Here is what I do

use File::MMagic; my $magicFile = '/usr/share/file/magic.mime'; my $mm = new File::MMagic->new($magicFile); my $filePath = $ARGV[0]; my $fh = new FileHandle "< $filePath"; my $res = $mm->checktype_filehandle($fh); print "$res\n";

I upload mp3 file via HTML form and then check its mime type. For the same file, it worked on one system, but failed on the other.

I get the following error

Invalid type 'regex' at line 271 Can't use an undefined value as an ARRAY reference at /usr/lib/perl5/s +ite_perl/5.8.8/File/MMagic.pm line 767, <GEN0> line 276.

I checked the Perl module version using

perl -MFile::MMagic -le 'print $File::MMagic::VERSION'

It showed 1.27 on both systems

if I do a uname -a, it gives me

For the system where it fails,

uname -a Linux xxx.com 2.6.18-238.12.1.el5 #1 SMP Tue May 31 13:22:04 EDT 2011 +x86_64 x86_64 x86_64 GNU/Linux

For the system where it works,

Linux xxx.com 2.6.18-028stab095.1-ent #1 SMP Mon Oct 24 20:45:18 MSD 2 +011 i686 i686 i386 GNU/Linux

I even ftp'd the mpeg file on which it is failing, to the system where it works. It did not have problems and determined the mime type fine.

What confuses me it that existing mp3 files work fine on both systems. But if I upload a file using http, it seems to be failing on one of the systems. It appears like files uploaded via http on one of the systems

Any suggestions as to why this may be happening. Thanks

Replies are listed 'Best First'.
Re: Problem with uploaded (http) files with File::MMagic
by CountZero (Bishop) on Dec 22, 2012 at 15:33 UTC
    Invalid type 'regex' at line 271
    The program you show only has 10 lines. Examine this line 271 and see what caused this error.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Actually, that is all of my 'test' program

      Line 271 appears to refers to /usr/share/file/magic.mime

      I do see that there are some differences in the magic.mime file for systems.

        Well it's a good thing you didn't show us the contents of line 271 of your /usr/share/file/magic.mime file. Otherwise we might have a clue to the problem. We wouldn't want that, would we?

        On a more serious note, I briefly looked over my local /usr/share/file/magic.mime file and didn't see anything particularly different in the region of line 271, but at line 303 in mine, I have the line:

        #0 regex BEGIN[[:space:]]*[{] application/x-awk

        and at line 640:

        0 regex [Cc]onstant[[:space:]]+[Ss]tory text/x-inform

        Those are the only two lines mentioning 'regex' in my file. Perhaps you could make a copy of your file and remove the regex lines and see if it works?

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Problem with uploaded (http) files with File::MMagic
by Anonymous Monk on Dec 22, 2012 at 17:36 UTC
    Sorry about not putting the cotents of usr/share/file/magic.mime at 271

    line 271 in that file had the following:

    0  regex       BEGIN[[:space:]]*[{]    application/x-awk

    As suggested (by roboticus), I took it out. I did not get the error, but I got the mime type as

    application/octet-stream

    instead of audio/mpeg

    I am thinking of going another way of finding the mime type. Changing system files to get it to work may not always work.

      You might try inserting the audio/mpeg line(s) from the magic.mime file that works into the magic.mime file that doesn't.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1010022]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-30 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found