-
-
Save reshadman/9056779 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 FileHandler { | |
/** | |
* allowable default file types | |
*/ | |
public $enabled_mimes = array ( | |
'image/png', | |
'image,jpg', #and what ever you want. | |
); | |
/** | |
* File address | |
*/ | |
public $file; | |
public function __construct($mimes=false,$file) | |
{ | |
if (is_array($mimes)){ | |
//replace if defined | |
$this->enabled_mimes = $mimes; | |
} | |
//set file address | |
/****** USE real_path() or getcwd() FOR ADDRESSING *******/ | |
$this->file = $file; | |
} | |
private function setError($error_message) | |
{ | |
//put error in session to use PRG pattern | |
//you should unset it after it is first views (flash session) | |
$_SESSION['file_type_error'] = $error_message; | |
} | |
/** | |
* @return boolean | |
*/ | |
public function processCheck($withError = "File type is not allowed") | |
{ | |
$FileInfo = new \finfo; | |
$mime = $FileInfo->file($this->file,FILEINFO_MIME); | |
if (!in_array($mime, $this->enabled_mimes)){ | |
//set a flash error message to session or cookie to display to user | |
$this->setError($withError); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage :