We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41165
    • 6 Posts
    I am using a posthook to process image files uploaded in a file field in the register snippt

    this is the code i have
    $user = $hook->getValue('register.user');
    $profile = $user->getOne('Profile');
    $max_file_size = 30000; // size in bytes 
    $max_height=100;
    $max_width=100;
    $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');  //allowed file name extensions
    $errors = array(1 => 'php.ini max file size exceeded', 
                    2 => 'html form max file size exceeded', 
                    3 => 'file upload was only partial', 
                    4 => 'no file was attached'); 
    
    $fieldname = 'profilePicture'; 
    
    //check for internal PHP errors with the file
    ($_FILES[$fieldname]['error'] == 0) 
        or hook->addError($errors[$_FILES[$fieldname]['error']], 'internal errors exist with the file'); 
    
    $uploadsDirectory  = MODX_BASE_URL."members/";
    
    //check to see if file was actually an image 
    @getimagesize($_FILES[$fieldname]['tmp_name']) 
        or hook->addError('Image', 'The uploaded file is not an image'); 
    
    //get the width and height of the image
    list($width, $height) = getimagesize($_FILES[$fieldname]['tmp_name']);  
    
     if(isset($width) && isset($height) && ($width >= $max_width || $height >= $max_height))
    {
        hook->addError('Image Size', 'Image size should be 100*100');
    }
    
    
    //make a unique filename for the file and check to see that it does not already exist
    $username=profile->get('username');
    $uploadFolderName=$uploadsDirectory.'$username';
    if(!is_dir ( $uploadFolderName )) 
        { 
        mkdir($uploadFolderName); 
        } 
      
    
    $uploadFileName=$uploadsDirectory.'profilePic';
    //move the file to the user images directory
    @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename) 
     
    // get the array of extended user profile attributes
    $extended = $profile->get('extended');
    $photo =$profile->get('photo');
    $photo=$uploadFilename
    $profile->set('photo', $photo);
    $profile->save();
    $user->save();
    return true;
     
    // add the value of the element for key `authorPhoto`
    /* 
    $extended['authorPhoto'] = $_FILES['userfile']['name'];
    $profile->set('extended', $extended);
    $profile->save();
    */
    


    after i submit a form the page just usually goes blank without the post hook everything is fine

    this is my snippet call
     [[!Register?&submitVar=`registerbtn`&submittedResourceId=`16` & postHooks=`uploadFiles` &usergroups=`Members` &activation=`1` &validate=`password2:password_confirm=^password^`]]
      • 3749
      • 24,544 Posts
      It looks like you could use a good code editor. wink

      A number of your variables are missing the opening $ token, several lines are missing their closing semicolon and you've spelled $uploadFilename two different ways.

      Try this:

      <?php
      /* @var $user modUser */
      $user = $hook->getValue('register.user');
      $profile = $user->getOne('Profile');
      $max_file_size = 30000; // size in bytes
      $max_height=100;
      $max_width=100;
      $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');  //allowed file name extensions
      $errors = array(1 => 'php.ini max file size exceeded',
      2 => 'html form max file size exceeded',
      3 => 'file upload was only partial',
      4 => 'no file was attached');
      
      $fieldname = 'profilePicture';
      
      //check for internal PHP errors with the file
      ($_FILES[$fieldname]['error'] == 0)
      or $hook->addError($errors[$_FILES[$fieldname]['error']], 'internal errors exist with the file');
      
      $uploadsDirectory  = MODX_BASE_URL."members/";
      
      //check to see if file was actually an image
      @getimagesize($_FILES[$fieldname]['tmp_name'])
      or $hook->addError('Image', 'The uploaded file is not an image');
      
      //get the width and height of the image
      list($width, $height) = getimagesize($_FILES[$fieldname]['tmp_name']);
      
      if(isset($width) && isset($height) && ($width >= $max_width || $height >= $max_height))
      {
      $hook->addError('Image Size', 'Image size should be 100*100');
      }
      



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting