First of all, sorry about my bad english, if you have some trouble understanding something that I have said, please ask.
Second: Thanks for the library! That’s what I was searching for a long time. Now I can use file and form validation in the same controller without problems.
I’m making a new version of my web and started using it for the file uploading forms(almost all are images) and for now i dont had any problem until today.
The thing is that I have learned that if you make a bmp file in the windows paint and change the extension from bmp to jpg the form_validation thinks that it’s a jpg, and it seems that the upload library of codeigniter has the same problem. So I have made a little change in the file_allowed_type function of your library to resolve that. Now instead of using the “type” parameter of the $_FILES variable, I make a new “type” parameter from the uploaded file and, using a MIME function of the upload library I compare the new “type” instead of using file extension comparations.
function file_allowed_type($file,$type)
{
//is type of format a,b,c,d? -> convert to array
$exts = explode(',',$type);
//is $type array? run self recursively
if(count($exts)>1)
{
foreach($exts as $v)
{
$rc = $this->file_allowed_type($file,$v);
if($rc===TRUE)
{
return TRUE;
}
}
}
//is type a group type? image, application, word_document, code, zip .... -> load proper array
$ext_groups = array();
$ext_groups['image'] = array('jpg','jpeg','gif','png');
$ext_groups['application'] = array('exe','dll','so','cgi');
$ext_groups['php_code'] = array('php','php4','php5','inc','phtml');
$ext_groups['word_document'] = array('rtf','doc','docx');
$ext_groups['compressed'] = array('zip','gzip','tar','gz');
$retorno = false;
//with this we get the TRUE MIME of the file, not the fake one
//'cause upload bug
$ftype = 'application/octet-stream';
$finfo = @new finfo(FILEINFO_MIME);
$fres = @$finfo->file($file['tmp_name']);
if (is_string($fres) && !empty($fres)) {
$tipo_archivo = $fres;
}
else
{
return False;
$this->set_message('file_allowed_type',"Error, tipo inexistente.");
}
$CI =& get_instance();
$CI->load->library('Upload');
$tipo = $ext_groups[$exts[0]];
foreach($tipo as $extension)
{
$mimes_internos = $CI->upload->mimes_types($extension);
if(is_array($mimes_internos))
{
if (in_array($tipo_archivo,$mimes_internos))
{
$retorno = true;
}
}
else
{
if ($mimes_internos == $tipo_archivo)
{
$retorno = true;
}
}
}
if($retorno == true)
{
return TRUE;
}
else
{
$this->set_message('file_allowed_type',"%s no puede ser del formato $tipo_archivo.");
return false;
}
}
Again, if you have some problem understanding me, ask whatever you want.