2016-06-09 7 views
0

Фор пример exif_imagetype() работает отличноПолучить MIME-тип URL

<?php echo exif_imagetype('http://orig01.deviantart.net/ace1/f/2010/227/4/6/png_test_by_destron23.png'); 

Но finfo_file() не работает.

<?php echo finfo_file(finfo_open(FILEINFO_MIME), 'http://orig01.deviantart.net/ace1/f/2010/227/4/6/png_test_by_destron23.png'); 

и получил

Warning: finfo_file(): Failed identify data 0:(null) in /test.php on line 1 

Любые мысли?

+0

да: почему вы думаете, файл информации будет работать? Тип mime - это значение заголовка, которое * может * быть отправлено сервером в ответ на HTTP-запрос GET, но нет гарантии, что сервер беспокоит его, включая его, поэтому вы получаете тип mime ресурса, выполняя сетевой запрос и проверяя HTTP ответ заголовка, и если нет заголовка типа mime, загрузка ресурса, расположенного по URL-адресу, а затем анализ фактического байт-кода –

+0

'exif_imagetype()' и 'finfo_file()' используют один и тот же метод php_stream_open_wrapper *() 'и чтение содержимого файла. – 66Ton99

ответ

0

Это нужно stream_wrapper_register()

class MimeStreamWrapper 
{ 
    const WRAPPER_NAME = 'mime'; 

    /** 
    * @var resource 
    */ 
    public $context; 

    /** 
    * @var bool 
    */ 
    private static $isRegistered = false; 

    /** 
    * @var callable 
    */ 
    private $callBackFunction; 

    /** 
    * @var bool 
    */ 
    private $eof = false; 

    /** 
    * @var resource 
    */ 
    private $fp; 

    /** 
    * @var string 
    */ 
    private $path; 

    /** 
    * @var array 
    */ 
    private $fileStat; 

    /** 
    * @return array 
    */ 
    private function getStat() 
    { 
     if ($fStat = fstat($this->fp)) { 
      return $fStat; 
     } 

     $size = 100; 
     if ($headers = get_headers($this->path, true)) { 
      $head = array_change_key_case($headers, CASE_LOWER); 
      $size = (int)$head['content-length']; 
     } 
     $blocks = ceil($size/512); 
     return array(
      'dev' => 16777220, 
      'ino' => 15764, 
      'mode' => 33188, 
      'nlink' => 1, 
      'uid' => 10000, 
      'gid' => 80, 
      'rdev' => 0, 
      'size' => $size, 
      'atime' => 0, 
      'mtime' => 0, 
      'ctime' => 0, 
      'blksize' => 4096, 
      'blocks' => $blocks, 
     ); 
    } 

    /** 
    * @param string $path 
    */ 
    public function setPath($path) 
    { 
     $this->path = $path; 
     $this->fp = fopen($this->path, 'rb') or die('Cannot open file: ' . $this->path); 
     $this->fileStat = $this->getStat(); 
    } 

    /** 
    * @param int $count 
    * @return string 
    */ 
    public function read($count) { 
     return fread($this->fp, $count); 
    } 

    /** 
    * @return string 
    */ 

    public function getStreamPath() 
    { 
     return str_replace(array('ftp://', 'http://', 'https://'), self::WRAPPER_NAME . '://', $this->path); 
    } 

    /** 
    * @return resource 
    */ 
    public function getContext() 
    { 
     if (!self::$isRegistered) { 
      stream_wrapper_register(self::WRAPPER_NAME, get_class()); 
      self::$isRegistered = true; 
     } 
     return stream_context_create(
      array(
       self::WRAPPER_NAME => array(
        'cb' => array($this, 'read'), 
        'fileStat' => $this->fileStat, 
       ) 
      ) 
     ); 
    } 

    /** 
    * @param $path 
    * @param $mode 
    * @param $options 
    * @param $opened_path 
    * @return bool 
    */ 
    public function stream_open($path, $mode, $options, &$opened_path) 
    { 
     if (!preg_match('/^r[bt]?$/', $mode) || !$this->context) { 
      return false; 
     } 
     $opt = stream_context_get_options($this->context); 
     if (!is_array($opt[self::WRAPPER_NAME]) || 
      !isset($opt[self::WRAPPER_NAME]['cb']) || 
      !is_callable($opt[self::WRAPPER_NAME]['cb']) 
     ) { 
      return false; 
     } 
     $this->callBackFunction = $opt[self::WRAPPER_NAME]['cb']; 
     $this->fileStat = $opt[self::WRAPPER_NAME]['fileStat']; 

     return true; 
    } 

    /** 
    * @param int $count 
    * @return mixed|string 
    */ 
    public function stream_read($count) 
    { 
     if ($this->eof || !$count) { 
      return ''; 
     } 
     if (($s = call_user_func($this->callBackFunction, $count)) == '') { 
      $this->eof = true; 
     } 
     return $s; 
    } 

    /** 
    * @return bool 
    */ 
    public function stream_eof() 
    { 
     return $this->eof; 
    } 

    /** 
    * @return array 
    */ 
    public function stream_stat() 
    { 
     return $this->fileStat; 
    } 

    /** 
    * @param int $castAs 
    * 
    * @return resource 
    */ 
    public function stream_cast($castAs) 
    { 
     $read = null; 
     $write = null; 
     $except = null; 
     return @stream_select($read, $write, $except, $castAs); 
    } 
} 

//$path = 'ftp://ftp.happy.kiev.ua/pub/pictures/ksyu/15.TIF'; 
//$path = 'ftp://ftp.happy.kiev.ua/pub/pictures/arcwolf1.gif'; 
//$path = 'ftp://ftp.happy.kiev.ua/pub/pictures/pub_k.pcx'; 
//$path = 'ftp://ftp.happy.kiev.ua/pub/pictures/9427.jpg'; 
//$path = 'https://homepages.cae.wisc.edu/~ece533/images/airplane.png'; 
//$path = '/etc/hosts'; 

$path = 'http://fc04.deviantart.net/fs71/f/2010/227/4/6/PNG_Test_by_Destron23.png'; 
echo "File: ", $path, "\n"; 
$wrapper = new MimeStreamWrapper(); 
$wrapper->setPath($path); 

$fInfo = new finfo(FILEINFO_MIME); 
echo "MIME-type: ", $fInfo->file($wrapper->getStreamPath(), FILEINFO_MIME_TYPE, $wrapper->getContext()), "\n";