Page 1 of 1

Mime Types

Posted: Wed Aug 10, 2005 4:26 pm
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?

Posted: Wed Aug 10, 2005 4:32 pm
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".

Posted: Wed Aug 10, 2005 6:08 pm
by Todd_Z
The file is already uploaded to the server. This isn't part of an upload script. Its post-upload processing.

Posted: Wed Aug 10, 2005 6:16 pm
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

Posted: Wed Aug 10, 2005 7:09 pm
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.

Posted: Wed Aug 10, 2005 7:19 pm
by neophyte
very useful thread. thanks feyd.

Posted: Wed Aug 10, 2005 8:47 pm
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 ]

Posted: Wed Aug 10, 2005 8:57 pm
by feyd
last I checked, that also used the extension to determine type information.

Posted: Wed Aug 10, 2005 9:15 pm
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

Posted: Thu Aug 11, 2005 12:04 pm
by bokehman
feyd wrote:last I checked, that also used the extension to determine type information.
Correct

Posted: Sat Aug 13, 2005 11:16 am
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?

Posted: Sat Aug 13, 2005 11:40 am
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.

Posted: Sat Aug 13, 2005 1:41 pm
by bokehman
Unless you just want jpeg images you could use imagecreatefromstring() much works with many image types.