Skip to content

Instantly share code, notes, and snippets.

@kinncj
Created January 10, 2017 17:05
Show Gist options
  • Save kinncj/c4ab19e4a468c2f82d89abcac6a94c2b to your computer and use it in GitHub Desktop.
Save kinncj/c4ab19e4a468c2f82d89abcac6a94c2b to your computer and use it in GitHub Desktop.
SplFileObject::getMimeType()
<?php
class FileObject extends \SplFileObject
{
private $size;
public function fwrite($content, $len = null) // :int
{
$this->size = $len ?: strlen($content);
if (!$len) {
return parent::fwrite($content);
}
return parent::fwrite($content, $len);
}
public function getMimeType() // :string
{
$finfo = new finfo(FILEINFO_MIME);
$this->fseek(0);
return $finfo->buffer(
$this->fread($this->size),
FILEINFO_MIME_TYPE
);
}
}
<?php
$f = file_get_contents('https://pbs.twimg.com/profile_images/655066410087940096/QSUlrrlm.png', false, null, -1, 1024);
$fo = new FileObject("php://temp/file.tmp", "w+");
$fo->fwrite($f);
var_dump($fo->getMimeType());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment