Add function for checking the mime type of a remove file

This commit is contained in:
Joshua Ramon Enslin 2021-05-13 22:18:58 +02:00
parent 63d6154d40
commit 20c33437c9
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE

View File

@ -576,6 +576,36 @@ final class MD_STD {
}
/**
* Wrapper around the finfo functions to get the mime content type of a file.
*
* @param string $filepath Expected file path.
*
* @return string
*/
public static function remote_mime_content_type(string $url):string {
if (($tmp_file = \tempnam(\sys_get_temp_dir(), "remote_mime_type_check")) === false) {
throw new Exception("Failed to get temporary file location");
}
$fp = \fopen($tmp_file, 'w');
if (!($ch = \curl_init($url))) {
throw new Exception("Failed to initialize curl for $url");
};
\curl_setopt($ch, CURLOPT_FILE, $fp);
\curl_exec($ch);
\curl_close($ch);
$mime_type = MD_STD::mime_content_type($tmp_file);
\unlink($tmp_file);
return $mime_type;
}
/**
* Checks if a file exists, with one of the expected mime types.
*