Determine Content-Type


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Determine Content-Type
# 1  
Old 08-23-2011
Determine Content-Type

I am limited to using only libraries that come with the default install of ActivePerl. I'd like to be able to determine the content type of a file on the fly. I thought CGI.pm might do it, but it looks a little weird to me from the perldoc. If someone has a quick way to determine the type of file, I'd appreciate it. A piece of sample code would be great.
# 2  
Old 08-23-2011
CGI doesn't tell you what a file is unless you're downloading the file. Even then, it can lie.

Determining what a file is isn't trivial when you aren't told. The file utility opens it and checks the first handful of bytes against a database of magic numbers:
Code:
$ file /bin/bash
/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
$

which you can use in perl with system() or `backticks`.
# 3  
Old 08-24-2011
Quote:
Originally Posted by Corona688
CGI doesn't tell you what a file is unless you're downloading the file. Even then, it can lie.

Determining what a file is isn't trivial when you aren't told. The file utility opens it and checks the first handful of bytes against a database of magic numbers:
Code:
$ file /bin/bash
/bin/bash: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
$

which you can use in perl with system() or `backticks`.
Thanks for the info. Does it support any type of formatting for when I'm using something like Net::SMTP? I'd like to make use of this for specifying if I'm using application/text, etc.
# 4  
Old 08-24-2011
If you'd tried checking its manual page, nearly the very first thing you'd see would be --mime-encoding.
# 5  
Old 08-24-2011
Quote:
Originally Posted by Corona688
If you'd tried checking its manual page, nearly the very first thing you'd see would be --mime-encoding.
Yup, I checked the man pages and the version of file I'm using on this Sparc machine only supports the -d, -c, -f, -h, -i, and -m flags. Sometimes, I'm a little limited to what I can use.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Facing issues with Content-Type:application/x-download Content-Disposition:attachment

I am in the process of developing a perl cgi page. I had succeeded in developing the page but there are few errors/issues with the page. description about cgi page: My CGI page retrieves all the file names from an directory and displays the files in drop down menu for downloading the... (5 Replies)
Discussion started by: scriptscript
5 Replies

2. Solaris

Solaris Cmd to determine type of zone?

I lost my notes on the subject, but I remember running across a single Solaris command that tells you the following Global zone vs local zone Sparse local zone vs Whole Root local zone Can anyone advise? Thanks-In-Advance!! (2 Replies)
Discussion started by: ckmehta
2 Replies

3. Shell Programming and Scripting

determine file type by content, not extension

I have a directory structure with multiple sub directories. Within each directory, there are files without extensions. Is it possible to somehow tell what the file type should be by the file contents? For example, I opened one of the files using an editor. After scrolling to the end of the... (2 Replies)
Discussion started by: daflore
2 Replies

4. HP-UX

How to determine the fie system type?

Good day I need create new mount points on a server. I'm not very familiar with HP-UX and was given steps on how to go about doing the mounting, etc. Before I start though, I need to determine which file system type is currently being used. This will determine whether i need to extend the... (3 Replies)
Discussion started by: notreallyhere
3 Replies

5. UNIX for Dummies Questions & Answers

How to determine what type of backup I've on the tape

I've tape which I've to use to restore data. The problem is that I don't know what type of backup I have on this type. Can someone help me and tell me how can I determine what type of backup is on the tape? Thanks for your help. (2 Replies)
Discussion started by: fraydey
2 Replies

6. Shell Programming and Scripting

Using 'stat' to determine file system type (with Zenity)

edited and removed (0 Replies)
Discussion started by: mdpalow
0 Replies

7. Shell Programming and Scripting

determine file type with perl

Hello i will like to know please how can i determine file type inside perl script not using the unix "file" program Thanks allot (1 Reply)
Discussion started by: umen
1 Replies

8. UNIX for Dummies Questions & Answers

is there a content type command?

Hi I run on a Unix webserver at my university. I have a couple of *.inc files which are include files used for php. They are all in one folder without an index file. If you go to the folder, you get to see a list of files in that dir. Some of the .inc have a question mark icon next to... (3 Replies)
Discussion started by: aqh2
3 Replies

9. AIX

Command to determine Tape Type

I have 2 UNIX boxes and I need to determine whether or not they possess the same tape drive. What is the hardware command to determine what type of tape drive is present? Thx in advance. (4 Replies)
Discussion started by: Surdeymon
4 Replies

10. UNIX for Dummies Questions & Answers

Content-Type

hey.. found out what was wrong. didnt have Read permission of the file for others LOL. But yeah the C version of it, why does it work in Netscape and not IE? primal ----- Hey everyone! Has anyone run into this before... I'm creating a UNIX CGI program that will parse $QUERY_STRING and save... (1 Reply)
Discussion started by: primal
1 Replies
Login or Register to Ask a Question