Mime Types
Moderator: General Moderators
Mime Types
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?
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.
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
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.
Code: Select all
man fileFILE(1) Copyrighted but distributable
NAME
file - determine file type
SYNOPSIS
file [ -bcikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] file ...
file -C [ -m magicfile ]
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 102Okay, 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?
I guess i answered my own question:
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.
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;
}
?>