Php-mysql Course

PHP 5.3.0 and later have finfo class with methods to get the content type and encoding of a file or string content, by looking for certain magic byte sequences at specific positions within that file /content.
- The getMimeType() function presented in this page uses the finfo class to return the Mime Type of a file, or a String content, in PHP.
This function is useful if you want to output a content with a header() that contains the correct Content-Type.
- Click on the code to select it.
function getMimeType($r, $t='file') {
//Returns the Mime Type of a file or a string content - from:
// $r = the resource: Path to the file; Or the String content
// $t = type of the resource, needed to be specified as "str" if $r is a string-content
  $finfo = new finfo(FILEINFO_MIME_TYPE);
  return ($t =='str') ? $finfo->buffer($r) : $finfo->file($r);
- If you want to get the mime-type of a file, just call the getMimeType() function with the path of that file.
- To get the mime-type of a string-content, call this function with the string-content and "str" as the 2nd argument.

• Examples:
1. Set header with the Content-Type of a file on server.
// here add the getMimeType() function

$file = 'path_to/file.pdf';
$mime_type = getMimeType($file);

// set the header and outputs the file
header('Content-Type: '. $mime_type);
2. Set header with the mime-type of a content from an URL address.
// here add the getMimeType() function

$url = '';
$cnt = file_get_contents($url);  //gets the string content
$mime_type = getMimeType($cnt, 'str');  //get the mime-type of the content in $cnt

// set the header and outputs the content
header('Content-Type: '. $mime_type);
echo $cnt;

Daily Test with Code Example

Click on the tag that creates a cell in table
<tr> <span> <td>
Indicate the CSS property used to specify a background image for an element
background-color background-image color
h3 {
  background-image: url("image.jpg");
What instruction can be used to parse all the array items?
for() [) object()
var arr = [1, "ab", ""];
for(var i=0; i< arr.length; i++) { alert(arr[i]); };
Indicate the PHP instruction used to traverse an associative array.
for() foreach() if()
$arr =["k1"=>"v1", "k2"=>"v2", "k3"=>"v3");
foreach($arr AS $k => $v) { echo "<br/>". $k ." - ". $v; }
Get Mime Type of file or string content in PHP

Last accessed pages

  1. CSS Course - Free lessons (13371)
  2. Download PHP-MySQL resources (712)
  3. Get and change IFrame content through a JavaScript script created in another IFrame (11077)
  4. Create simple Website with PHP (28920)
  5. Functions - scope, arguments and recursive (2157)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (810)
  2. querySelector and querySelectorAll (438)
  3. Read Excel file data in PHP - PhpExcelReader (404)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (378)
  5. JavaScript Course - Free lessons (291)