⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to validate the format of the image to upload?



dontako

dontako
  • profile picture
  • Member

Posted 24 October 2012 - 13:54 PM

Hello, I'm new in Grocery CRUD, I installed it and I loved it, it is easy to learn and implement, but I found a question about file uploading.

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! :lol:

noskov.biz

noskov.biz
  • profile picture
  • Member

Posted 25 October 2012 - 07:28 AM

Hi dontako and welcome to the forum!
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!

dontako

dontako
  • profile picture
  • Member

Posted 31 October 2012 - 19:13 PM

Thank you very much works great!! :) but I have a question.

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

noskov.biz

noskov.biz
  • profile picture
  • Member

Posted 31 October 2012 - 20:04 PM

You are welcome!
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 :P

dontako

dontako
  • profile picture
  • Member

Posted 31 October 2012 - 20:52 PM

I had thought something similar, your answer helped me a lot, thanks!

Greetings!

rubieruth

rubieruth
  • profile picture
  • Member

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. :(


victor

victor
  • profile picture
  • Member

Posted 06 February 2013 - 09:17 AM

Try to set a config value before GC initialization. For example: step 1: load the grocery crud config. step 2: Set the grocery_crud_file_upload_allow_file_types as you like. step 3: $crud = new geocery_crud.

ezgoen

ezgoen
  • profile picture
  • Member

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