note 119467 added to function.finfo-file

From: Date: Tue, 14 Jun 2016 18:33:36 +0000
Subject: note 119467 added to function.finfo-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-205723@lists.php.net to get a copy of this message
The way HOWTO get MIME-type of remote file. <?php class MimeStreamWrapper { const WRAPPER_NAME = 'mime'; public $context; private static $isRegistered = false; private $callBackFunction; private $eof = false; private $fp; private $path; private $fileStat; 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, ); } public function setPath($path) { $this->path = $path; $this->fp = fopen($this->path, 'rb') or die('Cannot open file: ' . $this->path); $this->fileStat = $this->getStat(); } public function read($count) { return fread($this->fp, $count); } public function getStreamPath() { return str_replace(array('ftp://', 'http://', 'https://'), self::WRAPPER_NAME . '://', $this->path); } 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, ) ) ); } 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; } public function stream_read($count) { if ($this->eof || !$count) { return ''; } if (($s = call_user_func($this->callBackFunction, $count)) == '') { $this->eof = true; } return $s; } public function stream_eof() { return $this->eof; } public function stream_stat() { return $this->fileStat; } public function stream_cast($castAs) { $read = null; $write = null; $except = null; return @stream_select($read, $write, $except, $castAs); } } $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"; ?> ---- Server IP: 72.52.91.14 Probable Submitter: 89.162.139.19 ---- Manual Page -- http://php.net/manual/en/function.finfo-file.php Edit -- https://master.php.net/note/edit/119467 Del: integrated -- https://master.php.net/note/delete/119467/integrated Del: useless -- https://master.php.net/note/delete/119467/useless Del: bad code -- https://master.php.net/note/delete/119467/bad+code Del: spam -- https://master.php.net/note/delete/119467/spam Del: non-english -- https://master.php.net/note/delete/119467/non-english Del: in docs -- https://master.php.net/note/delete/119467/in+docs Del: other reasons-- https://master.php.net/note/delete/119467 Reject -- https://master.php.net/note/reject/119467 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205723) next »