Skip to content

Instantly share code, notes, and snippets.

@MauMaGau
Created November 27, 2012 13:31
Show Gist options
  • Save MauMaGau/4154234 to your computer and use it in GitHub Desktop.
Save MauMaGau/4154234 to your computer and use it in GitHub Desktop.
Codeigniter allow file types except...
<?php if( ! defined('BASEPATH') ) exit('No direct script access allowed');
/*
If we allow all file type, allow for exclusions as well
*/
class MY_Upload Extends CI_Upload{
public function set_allowed_types($types)
{
$this->allowed_types = explode('|', $types);
if ( count($this->allowed_types) === 1 && in_array('*', $this->allowed_types))
{
$this->allowed_types = '*';
return;
}
}
function is_allowed_filetype($ignore_mime=FALSE){
$ext = strtolower(ltrim($this->file_ext, '.'));
if($this->allowed_types === '*'){
return TRUE;
}
if (in_array('*', $this->allowed_types))
{
if( in_array($ext, $this->allowed_types)){
return FALSE;
}
// Get the mime types that the file extension should refer to
$mime = $this->mimes_types($ext);
// If the extension relates to the mime type, then allow the file
if (is_array($mime))
{
if (in_array($this->file_type, $mime, TRUE))
{
return TRUE;
}
}
elseif ($mime == $this->file_type)
{
return TRUE;
}
return TRUE;
}
if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
{
$this->set_error('upload_no_file_types');
return FALSE;
}
if ( ! in_array($ext, $this->allowed_types))
{
return FALSE;
}
// Images get some additional checks
$image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
if (in_array($ext, $image_types))
{
if (getimagesize($this->file_temp) === FALSE)
{
return FALSE;
}
}
if ($ignore_mime === TRUE)
{
return TRUE;
}
$mime = $this->mimes_types($ext);
if (is_array($mime))
{
if (in_array($this->file_type, $mime, TRUE))
{
return TRUE;
}
}
elseif ($mime == $this->file_type)
{
return TRUE;
}
return FALSE;
}
}
<?php if( ! defined('BASEPATH') ) exit('No direct script access allowed');
/*
If we allow all file type, allow for exclusions as well
*/
class MY_Upload Extends CI_Upload{
public function set_allowed_types($types)
{
$this->allowed_types = explode('|', $types);
if ( count($this->allowed_types) === 1 && in_array('*', $this->allowed_types))
{
$this->allowed_types = '*';
return;
}
}
function is_allowed_filetype($ignore_mime=FALSE){
$ext = strtolower(ltrim($this->file_ext, '.'));
if($this->allowed_types === '*'){
return TRUE;
}
if (in_array('*', $this->allowed_types))
{
if( in_array($ext, $this->allowed_types)){
return FALSE;
}
// Get the mime types that the file extension should refer to
$mime = $this->mimes_types($ext);
// If the extension relates to the mime type, then allow the file
if (is_array($mime))
{
if (in_array($this->file_type, $mime, TRUE))
{
return TRUE;
}
}
elseif ($mime == $this->file_type)
{
return TRUE;
}
return TRUE;
}
if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
{
$this->set_error('upload_no_file_types');
return FALSE;
}
if ( ! in_array($ext, $this->allowed_types))
{
return FALSE;
}
// Images get some additional checks
$image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
if (in_array($ext, $image_types))
{
if (getimagesize($this->file_temp) === FALSE)
{
return FALSE;
}
}
if ($ignore_mime === TRUE)
{
return TRUE;
}
$mime = $this->mimes_types($ext);
if (is_array($mime))
{
if (in_array($this->file_type, $mime, TRUE))
{
return TRUE;
}
}
elseif ($mime == $this->file_type)
{
return TRUE;
}
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment