Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: how to read content-type from a file?

by Cubes (Pilgrim)
on Jul 23, 2001 at 18:14 UTC ( [id://99027]=note: print w/replies, xml ) Need Help??


in reply to how to read content-type from a file?

Some understanding of what a file "type" really is and how different systems deal with file types will probably help you here.

The thing you need to send to a browser, via the Content-Type header, is the MIME (Multipurpose Internet Mail Extensions) type. There are a number of RFCs dealing with various aspects of MIME, but the quick version is that this gives you a somewhat standardized way to say to the application on the other end of the internet, "I am sending you this type of data." Since there is a standard list of MIME types, the application on the other end (web browser, mailreader, whatever) can use this information to decide what to do with the data you've just sent. I found some good links to MIME information here.

Unfortunately, this MIME business came about long after various operating systems had already come up with their own ideas of file types, and their own ways of figuring out what to do with a particular file when you tell the OS to open or execute the file. On Windows systems, file types are determined by the filename extension. On *ix systems, there is a combination of filesystem attributes (for "special" files), filename extensions, and a few bits of special data at the beginning of executable files. Macs store (or used to, I haven't touched one in a while) the file type in with other filesystem data. These OS file types have little or nothing to do with MIME types.

So what you need to do is translate the "file type" from however it's stored on your system into a standard MIME type. The Apache web server does this by looking up the filename extension in its mime.types file (in the apache/conf/ directory). If you're running apache, you could do your lookup using this same file. If not, you could borrow the file from an apache distribution, or you could make your own. Regardless of where you get your lookup table, you'll have to have some definite way of determining the type of the source file. The simplest (for you) way of doing this is to simply require, as the web server does, that all of your downloadable files have a filename extension appropriate to their type. Another option is to keep a database of information about your downloadable files, including the file name and the MIME type for each file, and ignore the filename extension. There are also ways of getting the web server to do the work for you, like redirecting the download request form your script to the actual URL of the file (the downside here is that this makes it possible for people who figure out the URL pattern of download files to request files directly, bypassing your authorization/logging/whatever CGI script).

  • Comment on Re: how to read content-type from a file?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-09-18 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (24 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.