Jump to content


How to validate the format of the image to upload?

validate image upload validation

  • Please log in to reply
7 replies to this topic

#1 dontako

dontako

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts
  • LocationSantiago

Posted 24 October 2012 - 01: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:
Posted Image

#2 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

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!

#3 dontako

dontako

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts
  • LocationSantiago

Posted 31 October 2012 - 07: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, News and Music.

- In News I should only upload images, but due to the configuration files, I can upload an mp3.
- In music 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 Image

#4 noskov

noskov

    grocery CRUD Guru

  • Advanced Member
  • PipPipPip
  • 175 posts
  • LocationKiev

Posted 31 October 2012 - 08: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

#5 dontako

dontako

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts
  • LocationSantiago

Posted 31 October 2012 - 08:52 PM

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

Greetings!
Posted Image

#6 rubieruth

rubieruth

    Newbie

  • Members
  • Pip
  • 2 posts

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



#7 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

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.

Edited by victor, 06 February 2013 - 09:34 AM.


#8 ezgoen

ezgoen

    Member

  • Members
  • PipPip
  • 28 posts
  • LocationHobart, Australia

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/...nts/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







Also tagged with one or more of these keywords: validate, image, upload, validation

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users