From 20c33437c99b9606daf2d9dd092e304e742b9e67 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Thu, 13 May 2021 22:18:58 +0200 Subject: [PATCH] Add function for checking the mime type of a remove file --- src/MD_STD.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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. *