CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Limit Upload file size for custom file fields
Pages: [1]

Author Topic: Limit Upload file size for custom file fields  (Read 1845 times)

civicrmsav

  • I post frequently
  • ***
  • Posts: 121
  • Karma: 5
  • CiviCRM version: 4.2.6
  • CMS version: Drupal 7.x
Limit Upload file size for custom file fields
April 03, 2012, 06:22:06 pm
Hi -

I'm using a custom field to upload an image.   I don't see any way to limit the file size. 

I see that a similar question was asked here: http://forum.civicrm.org/index.php/topic,21784.msg91239.html#msg91239 with no response.

Thanks for any suggestions.

On this site I'm using Civi 4.1

penguintrax

  • I post frequently
  • ***
  • Posts: 230
  • Karma: 2
  • CiviCRM version: 3.3x and 4.x depending on site
  • CMS version: Joomla 1.5 or 1.7 depending on site
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.17
Re: Limit Upload file size for custom file fields
April 05, 2012, 12:25:37 pm
I asked the question originally. I still need an answer. I hope we find one this time.

demeritcowboy

  • Ask me questions
  • ****
  • Posts: 570
  • Karma: 42
  • CiviCRM version: Always the latest!
  • CMS version: Drupal 6 mostly, still evaluating 7.
  • MySQL version: Mix of 5.0 / 5.1 / 5.5
  • PHP version: 5.3, usually on Windows
Re: Limit Upload file size for custom file fields
April 05, 2012, 01:40:27 pm
Try the validate hook:
http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+hook+specification#CiviCRMhookspecification-hookcivicrmvalidate

civicrmsav

  • I post frequently
  • ***
  • Posts: 121
  • Karma: 5
  • CiviCRM version: 4.2.6
  • CMS version: Drupal 7.x
Re: Limit Upload file size for custom file fields
May 15, 2012, 08:46:40 pm
Thanks for the suggestion -

I came up with the code below using the validate hook.  It works, but I realize now that I really want this to happen before the form is submitted - part of the problem is that impatient users try to upload very large files and think that the site is broken.  I looked around a bit and it seems that this is not trivial to do if you want to support IE as well as older browsers - requiring something like uploadify.  I don't know how that would or could be integrated into civi.  I tried to look at how CCK does it, and was surprised to find that (at least on my installation) d7's image field allows you to set a max file size, but doesn't enforce it.  So for example, I set the max file size for a particular field in a content type as 1 MB, and I see the text "Files must be less than 1 MB" when I add or edit a node of that type,  but I can upload files of 5MB or more, up to the server limit.

I'm sharing the code below mostly for the benefit of penguintrax, in case this is helpful for that use case.

(Actually this code only almost works, I get the message "Warning: array_merge() [function.array-merge]: Argument #2 is not an array in CRM_Utils_Hook->runHooks() (line 125 of /home4/uppermad/public_html/sites/all/modules/civicrm/CRM/Utils/Hook.php)."  on subsequent pages.  I haven't figured out why.)

 
Code: [Select]
<?php

/**
 * The maximum allowed file size.
 */
define('MAX_ALLOWED_FILE_SIZE', 2097152); //2 megabytes in bytes

/**
 * Check file sizes
 * true if form validates successfully, otherwise array with input field names as keys and error message strings as values
 */
 
function mymodule_civicrm_validate( $formName, &$fields, &$files, &$form ){
  
$errors = array();
  if (
$formName == 'CRM_Event_Form_Registration_Register') {
  
    
foreach ($files as $key => $file){
   if($file['size']>=MAX_ALLOWED_FILE_SIZE){
   $errors[$key] = ts('The maximum file size allowed is '. round((MAX_ALLOWED_FILE_SIZE/1048576),2) .' MB. '. $file['name'] .' is about '. round(($file['size'] * 9.5367431640625e-7),2) .' MB');
   }
   }
} 
  return empty(
$errors) ? true : $errors;
}

penguintrax

  • I post frequently
  • ***
  • Posts: 230
  • Karma: 2
  • CiviCRM version: 3.3x and 4.x depending on site
  • CMS version: Joomla 1.5 or 1.7 depending on site
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.17
Re: Limit Upload file size for custom file fields
May 21, 2012, 02:34:25 pm
Thanks! This def give me something to chew on with my dev guy.  If we make further progress, I'll let you know.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • Limit Upload file size for custom file fields

This forum was archived on 2017-11-26.