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: https://coursesweb.net/
// $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);
readfile($file);
exit;
2. Set header with the mime-type of a content from an URL address.
// here add the getMimeType() function

$url = 'https://coursesweb.net/imgs/coursesweb.png';
$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;
exit;

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Get Mime Type of file or string content in PHP

Last accessed pages

  1. Shape Tween - Flash Animation (6149)
  2. The Mastery of Love (7440)
  3. Get Mime Type of file or string content in PHP (6230)
  4. Countdown Timer with starting time added into a form (11533)
  5. Disable button and Enable it after specified time (17533)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)