how to get correct file or content mime type using 2fin php

Solutions on MaxInterview for how to get correct file or content mime type using 2fin php by the best coders in the world

showing results for - "how to get correct file or content mime type using 2fin php"
Mila
28 Aug 2019
1echo mime_content_type("yourfile.gif"); //image/gif
Niklas
01 Jan 2021
1function get_content_mime_type($content)
2{
3  $url = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types';
4  $url_live = false;
5  $handle = curl_init($url);
6  curl_setopt_array($handle, array(
7    CURLOPT_FOLLOWLOCATION => true,
8    CURLOPT_NOBODY => true,
9    CURLOPT_HEADER => false,
10    CURLOPT_RETURNTRANSFER => false,
11    CURLOPT_SSL_VERIFYHOST => false,
12    CURLOPT_SSL_VERIFYPEER => false
13  ));
14  $response = curl_exec($handle);
15  $httpCode = curl_getinfo($handle, CURLINFO_EFFECTIVE_URL);
16  $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
17  if ($httpCode == 200)
18  {
19    $url_live = true;
20  }
21  $url_live = $url_live;
22  curl_close($handle);
23  $mimes = array();
24  if ($url_live)
25  {
26    $mimes_file = file_get_contents($url);
27    preg_match_all('#^([^\s]{2,}?)\s+(.+?)$#ism', $mimes_file, $matches, PREG_SET_ORDER);
28    foreach ($matches as $match)
29    {
30      $exts = explode(" ", $match[2]);
31      foreach ($exts as $ext)
32      {
33        $mimes[$ext] = $match[1];
34      }
35    }
36  }
37  else
38  {
39    $mimes = array(
40      'txt' => 'text/plain',
41      'htm' => 'text/html',
42      'html' => 'text/html',
43      'php' => 'text/html',
44      'css' => 'text/css',
45      'js' => 'application/javascript',
46      'json' => 'application/json',
47      'xml' => 'application/xml',
48      'swf' => 'application/x-shockwave-flash',
49      'flv' => 'video/x-flv',
50      // images
51      'png' => 'image/png',
52      'jpe' => 'image/jpeg',
53      'jpeg' => 'image/jpeg',
54      'jpg' => 'image/jpeg',
55      'gif' => 'image/gif',
56      'bmp' => 'image/bmp',
57      'ico' => 'image/vnd.microsoft.icon',
58      'tiff' => 'image/tiff',
59      'tif' => 'image/tiff',
60      'svg' => 'image/svg+xml',
61      'svgz' => 'image/svg+xml',
62      // archives
63      'zip' => 'application/zip',
64      'rar' => 'application/x-rar-compressed',
65      'exe' => 'application/x-msdownload',
66      'msi' => 'application/x-msdownload',
67      'cab' => 'application/vnd.ms-cab-compressed',
68      // audio/video
69      'mp3' => 'audio/mpeg',
70      'qt' => 'video/quicktime',
71      'mov' => 'video/quicktime',
72      // adobe
73      'pdf' => 'application/pdf',
74      'psd' => 'image/vnd.adobe.photoshop',
75      'ai' => 'application/postscript',
76      'eps' => 'application/postscript',
77      'ps' => 'application/postscript',
78      // ms office
79      'doc' => 'application/msword',
80      'rtf' => 'application/rtf',
81      'xls' => 'application/vnd.ms-excel',
82      'ppt' => 'application/vnd.ms-powerpoint',
83      'docx' => 'application/msword',
84      'xlsx' => 'application/vnd.ms-excel',
85      'pptx' => 'application/vnd.ms-powerpoint',
86      // open office
87      'odt' => 'application/vnd.oasis.opendocument.text',
88      'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
89    );
90  }
91  $content_mime = 'unknown';
92  if (is_file($content))
93  {
94    if (isset(pathinfo($content) ['extension']))
95    {
96      $content_ext = pathinfo($content) ['extension'];
97      if (isset($mimes[$content_ext]))
98      {
99        $content_mime = $mimes[$content_ext];
100      }
101      else
102      {
103        if (is_readable($content) && is_executable($content))
104        {
105          $finfo = finfo_open(FILEINFO_MIME_TYPE);
106          $content_mime = finfo_file($finfo, $content);
107          if ($content_mime === null | $content_mime === "")
108          {
109            $content_mime = "application/octet-stream";
110          }
111          else
112          {
113            $content_mime = $content_mime;
114          }
115          finfo_close($finfo);
116        }
117        else
118        {
119          $content_mime = "application/octet-stream";
120        }
121      }
122    }
123  }
124  else
125  {
126    // return whatever you want
127    // $content_mime = 'unknown';
128
129  }
130  $content_mime = $content_mime;
131  return $content_mime;
132}
133
queries leading to this page
php check mime type of uploaded filewhich attribute depicts the mime type of file in phpphp get mime type imagephp get mime typephp check a file 27s mime typeget file extension php mimetypephp get mime type from fileget the file in php in storage and get the mime typeget mime type of file in phpget file mime tyep phpfileinfo mime type in phpphp download file mime typeget mime type of file 2b phpget mime type of file laravelphp check file mime typeget mime type of file upload laravelget mime type using file name phpcheck mime type from file tmp phphow to get content mime type using phpphp get file mimetypeget mime type of file path in phpphp get file mime type from fileusing file name get mime type in phpphp file mime type from file nameget mime type of file path phpget file mime type phpphp get file mime from pathhow to get mime type using file name phpphp get mime type from extensionphp file mime typeget file extension from mime type 2b phpphp get mime type from file nameget mime type of file phpget mime type from file phpcheck file mime type in phpget mime of file phpphp get mime type of uploaded filehow to check mime type in phpphp get all mime typesget mime type file php postphp get mime type from urlget file mime type 2b phpphp get file extension by mime typecheck mime of input uploaded phpget mime type phphow to get mime type of a content or a file in phpget file mimetype phpphp file mimephp get extension from content typephp check file mime type imagepathinfo 28 29 2b get file mime type 2b phpphp get file mime content typehow to get file mime type in phpphp get mime typephp get image mime typeshow to get file stream mime type in phpget mime types phpget mime type of file in storage in phpmime type file phpget mime type of uploaded file phpphp mime type from filenamephp get file extension from mime typephp get mime type of a file without downloadphp get file content mime typehow to get correct file or content mime type using 2fin phpphp file upload mime typehow to get mime type using file content in phpphp get mime type of filemime type phpphp get image mime typeget mime type of file laravel 8check mime type of file phpdetermine mime type of file phpphp geg mime type from filefunction to get file type by mime phpcheck file mime type phpuse mime to get file extension phpphp file get mime typephp get mime type of local filephp get uploaded file mime typemime type srt file phpphp how to know readfile mime typelaravel get file mime typephp fileinfo mime typemime type for php filephp get mime type of file before uploadphp check uploaded file mime type imagehow to get mime type of file in phpphp get file mime typeget mime image phpphp get file mimephp get mime type from filenamephp get mime type from contentget mimetype of file phpphp file with image mime typeget mimetype of a file php php check mime typephp set mime type of filehow to get mime type using content in phpphp get mime type from file get contenthow to get content or file mime type in phplaravel get mime type of uploaded filefile mime type phpget mime type of file in laravelget mime type from file name phphow to get correct file or content mime type using 2fin php