Skip to content

Instantly share code, notes, and snippets.

@aertmann
Created November 16, 2012 13:10
Show Gist options
  • Save aertmann/4087227 to your computer and use it in GitHub Desktop.
Save aertmann/4087227 to your computer and use it in GitHub Desktop.
$this->isAllowedFile($f['particle_filter_document']['type'], $f['particle_filter_document']['size'], 'particle_filter_document',$f['registration_form_document']['name'])
/**
* Verify the filetype is valid according to the TCA definition
*
* @param string $fileMimeType
* @param string $fileSize
* @param string $fieldName
* @param string $name
*
* @return boolean
*/
protected function isAllowedFile($fileMimeType, $fileSize, $fieldName, $name) {
$fileInfoArray = t3lib_div::split_fileref($name);
$extension = $fileInfoArray['fileext'];
//var_dump($fileMimeType . " " . $extension . " " . $fileSize);die("filetest");
$isValid = TRUE;
$errorText = 'temp';
$errorCode = 0;
// type mime array
$mimeTypeArray = array(
'application/msword' => 'allowed',
'application/pdf' => 'allowed',
'application/x-pdf' => 'allowed',
'application/acrobat' => 'allowed',
'application/vnd.pdf' => 'allowed',
'text/pdf' => 'allowed',
'text/x-pdf' => 'allowed',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'allowed',
'image/bmp' => 'allowed',
'image/gif' => 'allowed',
'image/jpeg' => 'allowed',
'image/jpeg' => 'allowed',
'image/pjpeg' => 'allowed',
'image/png' => 'allowed',
'image/x-png' => 'allowed',
'application/rtf' => 'allowed',
'application/octet-stream' => 'allowed',
'application/vnd.oasis.opendocument.text' => 'allowed'
);
// check for allowed type & extensions
if (array_key_exists($fileMimeType, $mimeTypeArray)) {
$allowedExtension = $mimeTypeArray[$fileMimeType];
// get the allowed extenstions from TCA
$allowedFromTCA = explode(',', $GLOBALS['TCA']['tx_applusecolabel_vehicle']['columns'][$fieldName]['config']['allowed']);
if (!in_array($extension, $allowedFromTCA)) {
$isValid = FALSE;
}
} else {
$isValid = FALSE;
}
if ($isValid === FALSE) {
// filetype is wrong
$errorText = 'File type not allowed';
$errorCode = 1307458284;
} else {
// type is valid above - check for size
$allowedMaxSize = $GLOBALS['TCA']['tx_applusecolabel_vehicle']['columns'][$fieldName]['config']['max_size'] * 1024; // KB -> Bytes
if ($fileSize > $allowedMaxSize) {
$errorText = 'File size is too large';
$errorCode = 1307517738;
$isValid = FALSE;
}
}
if ($isValid === FALSE) {
$error = t3lib_div::makeInstance('Tx_Extbase_Validation_Error', $errorText, $errorCode);
$this->validationManager->addError($error, $fieldName . '_error');
return FALSE;
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment