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 defines the clickable areas inside the image map?
<map> <img> <area><img src="image.jpg" usemap="#map1">
<map name="map1">
<area shape="rect" coords="9, 120, 56, 149" href="#">
<area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position#id {
overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseoutdocument.getElementById("id").onmouseover = function(){
document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POSTif(isset($_GET["id"])) {
echo $_GET["id"];
}