Showing posts with label PHP Upload. Show all posts
Showing posts with label PHP Upload. Show all posts

Thursday, September 27, 2012

Get MIME type


If you looking for a solution to find the File Content-Type or MIME Type.
Detecting MIME Type of each file extension is necessary to build dynamic file processing in PHP

Example:

// In the following code image/png is MIME type which is assigned dynamically
header("Content-Type:image/png"); 


We can get MIME Type using the following simple php code


Here is the best solution to find the Exact MIME Type of the file using the following simple upload form.

<?php
session_start();

if(isset($_POST['upload']))
{
 $mime_type =  $_FILES['upload_file']['type'];
 $_SESSION['mime_type'][] = $mime_type;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload and Resize Image</title>
<style type="text/css">
body,td,th {
 font-size: 14px;
 font-family: Verdana, Geneva, sans-serif;
}
body {
 margin-left: 10px;
 margin-right: 10px;
}
</style>
</head>
<body>

<form action="" method="post" enctype="multipart/form-data">
    <fieldset>
    <legend>Upload File in PHP</legend>
    
        <input name="upload_file" type="file" id="upload_file" />
        
        <input name="upload" type="submit" id="upload" value="Upload" />
        
    </fieldset>
</form>

<div style="padding:10px;">
Uploaded File MIME Type is : <br />
<pre>
<?php 
if(is_array(@$_SESSION['mime_type']))
foreach($_SESSION['mime_type'] as $type)
echo @$type."\n"; ?>
</pre>
</div>

</body>
</html>


Download This Script     Live Demo