Skip to content

Instantly share code, notes, and snippets.

@gaffling
Created April 3, 2021 12:32
Show Gist options
  • Save gaffling/b4072aa3ef7a10e196a81a2ad47f23b1 to your computer and use it in GitHub Desktop.
Save gaffling/b4072aa3ef7a10e196a81a2ad47f23b1 to your computer and use it in GitHub Desktop.
[Check Mime Type] Check if URl is a given MimeType #php #function #mime
/* ----------------------------------------------------------------------- */
/* [Check Mime Type] Check if URl is a given MimeType #php #function #mime */
/* ----------------------------------------------------------------------- */
function checkMime($url, $mime='application/pdf') {
// MP3 = 'audio/mpeg' - PDF = 'application/pdf'
if (!function_exists('curl_init')) die('ERROR - Please install CURL on your PHP!');
$a = parse_url($url);
if (checkdnsrr(str_replace('www.','',$a['host']),'A') or checkdnsrr(str_replace('www.','',$a['host']))) {
$ch = @curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$results = explode(PHP_EOL, trim(curl_exec($ch)));
var_dump($results);
$check = '';
foreach ($results as $line) {
if (strtolower(strtok($line, ':')) == 'content-type') {
$parts = explode(':', $line);
$check = trim($parts[1]);
}
}
return $check==$mime;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment