How to validate the format of the image to upload?
- Single Page
Posted 24 October 2012 - 13:54 PM
I just want to upload images, but should only be JPG, but the system allows me to upload anything (DOC, PDF, etc. ..). Reading found that a user made a change to the library to do, but it was in 1.2
Is this already implemented in version 1.3.2? (It's the one I downloaded), how I can implement it?
Greetings and thanks!
Posted 25 October 2012 - 07:28 AM
If you want to upload a file with specific extension, please, go to the ./application/config/grocery_crud.php config file and find the line #11
$config['grocery_crud_file_upload_allow_file_types'] = 'gif|jpeg|jpg|png|tiff|doc|docx|txt|odt|xls|xlsx|pdf|ppt|pptx|pps|ppsx|mp3|m4a|ogg|wav|mp4|m4v|mov|wmv|flv|avi|mpg|ogv|3gp|3g2';
there you can find allowed extensions for the uploading and change it what you need.
Best regards!
Posted 31 October 2012 - 19:13 PM
I have the following case:
$ config ['grocery_crud_file_upload_allow_file_types'] = 'jpg | mp3';
I have 2 modules, [b]News [/b]and [b]Music[/b].
- In [b]News [/b]I should only upload images, but due to the configuration files, I can upload an mp3.
- In [b]music [/b]I should only upload mp3, but it's the same, and allows me to upload images.
You can limit per module? eg some set_rules() or should I create my own validator in a callback?
regards
Posted 31 October 2012 - 20:04 PM
I think that you can make it using callback_before_upload. Please read the docs for more info.
It would be something like this in your function:
$crud->callback_before_upload(array($this, '_valid_images'));
and the callback:
public function _valid_images($files_to_upload, $field_info)
{
if ($files_to_upload[$field_info->encrypted_field_name]['type'] != 'image/png')
{
return 'Sorry, we can upload only PNG-images here.';
}
return true;
}
And the similar code can be for your Music section, just check another file type 'audio/mp3'.
P.S. Please, check my example, cause I don't know for sure whether it works properly
Posted 31 October 2012 - 20:52 PM
Greetings!
Posted 06 February 2013 - 06:25 AM
Good Day.
we use the code above in our project and it works for sinlge image extension..how to declare in if statement for multiple image extension such as jpeg,jpg,gif,png?
Here's the code
public function valid_file($files_to_upload, $field_info)
{
if (($files_to_upload[$field_info->encrypted_field_name]['type'] != 'image/png') OR ($files_to_upload[$field_info->encrypted_field_name]['type'] != 'image/jpeg'))
{
return 'Sorry, we can upload only images here.';
}
return true;
}
please help. :(
Posted 06 February 2013 - 09:17 AM
Posted 25 March 2013 - 04:53 AM
Hi,
I tried that option but it didnt work - I checked that grocery_crud_file_upload_allow_file_types had my modified content - and it did - but the upload still allowed text and other files.
Here's what I tried:
function _init_upload_params(){
$this->config->grocery_crud_file_upload_allow_file_types = 'gif|jpeg|jpg|png|tiff';
$this->config->grocery_crud_file_upload_max_file_size = '20MB'; //ex. '10MB' (Mega Bytes), '1067KB' (Kilo Bytes), '5000B' (Bytes)
}
and I would l\call that prior to $crud = new grocery_CRUD();
I ended up using this little trick - according to http://www.iana.org/assignments/media-types all image types begin with "image/xyz"
here's the callback code:
public function _cb_valid_images($files,$field_info) {
$vtypes = $this->config->grocery_crud_file_upload_allow_file_types;
$mtype = $files[$field_info->encrypted_field_name]['type'];
$errorstr ="Sorry - Only Image files allowed ($vtypes)";
if ( stripos($mtype,'image') !== False ) {
if (stripos($vtypes,substr($mtype,6)) === False) {
return $errorstr;
}
} else {
return $errorstr;
}
}
Hope this helps someone..
Cheers
EZ