Skip to content

Instantly share code, notes, and snippets.

@reshadman
Forked from noondreams/gist:9049886
Last active August 29, 2015 13:56
Show Gist options
  • Save reshadman/9056779 to your computer and use it in GitHub Desktop.
Save reshadman/9056779 to your computer and use it in GitHub Desktop.
<?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;
}
}
@reshadman
Copy link
Author

usage :


$type = new FileHandler(false,'my/path/image.jpg');
if ($type->processCheck()){
   #success
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment