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 in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Get Mime Type of file or string content in PHP

Last accessed pages

  1. Integer and Float value in Select with PDO from string to numeric (8672)
  2. Get and change IFrame content through a JavaScript script created in another IFrame (16553)
  3. Shape Tween - Flash Animation (6185)
  4. CSS Border (6122)
  5. Disable button and Enable it after specified time (17587)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (524)
  2. CSS cursor property - Custom Cursors (70)
  3. The Mastery of Love (50)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (48)
  5. Read Excel file data in PHP - PhpExcelReader (46)