Mime Types

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Mime Types

Post by Todd_Z »

I have an attachment script that will react to different mime types, but i assume if I base the action on the file extension, it could be a huge security threat. Therefore, I want to create a function to determine the mime type of the file i give it. Any ideas how to do this?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

http://us3.php.net/features.file-upload
$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif".
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

The file is already uploaded to the server. This isn't part of an upload script. Its post-upload processing.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

if its an image, getimagesize index 2 will return the mime type

else

http://us3.php.net/manual/en/function.m ... t-type.php is what I could find quickly
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

although not apart of this conversation, since it was mentioned:

do not EVER rely on $_FILES['whatever']['type'] to be correct, or even set. It's very simple to send a false mime-type during an upload.

now, on topic:
mime_content_type() is file extension specific, it does not analyze the file for a binary signature. I have posted before how to analyze for certain types: viewtopic.php?t=23517




took me a while to find that, so I added it to the useful posts thread.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

very useful thread. thanks feyd.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

man file
FILE(1) Copyrighted but distributable

NAME
file - determine file type

SYNOPSIS
file [ -bcikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] file ...
file -C [ -m magicfile ]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

last I checked, that also used the extension to determine type information.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I think file is smarter than that ;)

Code: Select all

timvw@madoka:~$ touch blah.jpg
timvw@madoka:~$ file blah.jpg
blah.jpg: empty

timvw@madoka:~$ dd if=/dev/random of=blah.jpg count=20
0+20 records in
13+1 records out
6800 bytes transferred in 0.012150 seconds (559668 bytes/sec)
timvw@madoka:~$ file blah.jpg
blah.jpg: data

timvw@madoka:~$ cp src/timvw.info/images/php_dn_badge_i_am_the_manual.gif blah.jpg
timvw@madoka:~$ file blah.jpg
blah.jpg: GIF image data, version 89a, 52 x 102
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

feyd wrote:last I checked, that also used the extension to determine type information.
Correct
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Okay, so now that i got that down - how should i manage my attachments? I want to display the picture if its an image, show it if its a regular text file, and download it if its neither. I can do the show image and dump file easily, but is there a php function to force the download? Or do i just need to echo a hyperlink to the file?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I guess i answered my own question:

Code: Select all

<?	

	$loc = "/absolutepath/";

	$dir = opendir( $loc );
	while ( $f = readdir($dir) )
		if ( preg_match("#^{$_GET['q']}-#", $f ) )
			break;
	
	if ( !$f )
		die ( "No Attachment Found." );
		
	function getMIME ( $file ) {
		$ext = substr( $file, strrpos($file,".")+1 );
		if ( $ext == "jpg" ) return "image/jpeg";
	}
	
	$mime = getMIME( $f );
	
	switch ( $mime ) {
		case "image/jpeg":
			header( "Content-type: image/jpeg" );
			$img = imagecreatefromjpeg( $loc.$f );
			imagejpeg( $img, "", 100 );
			break;
		default:
			header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
			header( "Content-Type: application/octet-stream" );
			header( "Content-Length: ".filesize($loc.$f) );
			header( "Content-Disposition: attachment; filename=$f" );
			readfile($loc.$f);
			break;
	}

?>
If i either want to show an image or download the file, I will send it the $_GET['q'] value of the id for the attachment. Anyways, so yea - if its a jpg, view, if its anything else, download. I shall add more image types later, but you get the gist.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Unless you just want jpeg images you could use imagecreatefromstring() much works with many image types.
Post Reply