diff --git a/src/MD_STD.php b/src/MD_STD.php index 383bd3c..2f938c7 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -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. *