You are here:Home arrow Home arrow PHP arrow Getting MIME of remote file with Zend Framework
  • Narrow screen resolution
  • Wide screen resolution
  • Decrease font size
  • Default font size
  • Increase font size
  • default color
  • green color
  • blue color

Getting MIME of remote file with Zend Framework

Friday, 09 April 2010
If you ever need to get content type of remote file from within PHP, this class will come in handy. Supposing you need to check external link if it is an mp3 file. You will be able to do this: $sniffer = new Smartycode_Http_Mime();
$contentType = $sniffer->getMime($url);
if('audio/mpeg3' == $contentType) echo 'It is MP3';
<?php
/*
 * @author Danila Vershinin (smartycode.com)
 */

require_once 'Zend/Http/Client.php';
class Smartycode_Http_Mime {

  private static $client = null;

  public function getMime($url) {
    if (!self::$client) {
       self::$client = new Zend_Http_Client();
       self::$client->setConfig(array(
          'maxredirects' => 5,
          'timeout'      => 30,
          'strictredirects' => true));
    }

    try {
      self::$client->setUri($url);
    } catch (Exception $e) {
      Zend_Registry::get('logger')->debug($url. ' is not valid URL by W3C rules');
      return null;
    }
    //$client = new Zend_Http_Client($url);
    $mime = null;
    try {
      $response = self::$client->request('HEAD');
      $content_type = $response->getHeader('Content-Type');
      if (is_array($content_type) && count($content_type)) $content_type = $content_type[0];
      list($mime) = explode(';', $content_type);

    } catch (Exception $e) {
      Zend_Registry::get('logger')->debug('Error while getting mime type for '.$url);
      unset($response);
      unset($content_type);
     }
     unset($client);
    return $mime;
  }

}

?>




Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!
Last Updated ( Friday, 09 April 2010 )

Add comment


Security code
Refresh

< Prev   Next >