Question : Determine MIME types for files in php

In another follow-up from question http://www.experts-exchange.com/Web_Development/WebApplications/Q_26357020.html#a33291011.

I need to determine MIME types of my uploaded file so I can email it successfully. I am using php mailer and could not get it to mail correctly without specifying the MIME as in "$Mail->AddAttachment($path,$name,'base64','Application/msword');"

I am not sure how to get the extension from the $_FILES POST variable to make the determination for a decision statement.  I will create an IF-ELSE or CASE statement that will allow me to check the type so I can roll through and use the appropriate one. The users should only be uploading Word, PDF, Text, or other word processer format.

Thanks in advance

Answer : Determine MIME types for files in php

To know extension I found this nice function (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_26341600.html:):

<?
$file = explode(".", $_FILES['your_var_name']['name']);
$extension = array_pop($file);
?>

If you wish to limit file types user can upload there is a nice funciton here (http://php.net/manual/en/features.file-upload.php):

<?php

$file = $_FILES['userfile'];

$allowedExtensions = array("txt", "rtf", "doc");

function isAllowedExtension($fileName) {
  global $allowedExtensions;

  return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

if($file['error'] == UPLOAD_ERR_OK) {
  if(isAllowedExtension($file['name'])) {
    # Do uploading here
  } else {
    echo "Invalid file type";
  }
} else die("Cannot upload");

?>

Hope this helps
Random Solutions  
 
programming4us programming4us