Created
January 10, 2017 17:05
-
-
Save kinncj/c4ab19e4a468c2f82d89abcac6a94c2b to your computer and use it in GitHub Desktop.
SplFileObject::getMimeType()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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