Skip to content

Instantly share code, notes, and snippets.

@primitive-type
Created August 6, 2014 16:42
Show Gist options
  • Save primitive-type/1eb69794b6e25e23e091 to your computer and use it in GitHub Desktop.
Save primitive-type/1eb69794b6e25e23e091 to your computer and use it in GitHub Desktop.
Get MIME types for CDN hosted files
<?php
$fileUrl = 'http://t2.gstatic.com/images?q=tbn:ANd9GcS8ntYWjylXybJ4WIr5WvFJiknlXdS8jkhwpaRXqhCY3w2vFyuR-Q';
// Get the file's MIME type
// This method is used because many file URLs hosted on CDNs do not contain file extensions
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mediaType = $finfo->buffer(file_get_contents($fileUrl));
// Is this an image, audio, text, video, etc.?
$type = dirname($mediaType);
// Probably your best guess at the file extension when it's not contained in the URL
$subtype = basename($mediaType);
echo $mediaType;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment