Hooking into the file upload ajax process, How to Permanently Save File Upload of Type managed_file

Public

https://api.drupal.org/api/drupal/includes%21file.inc/group/file/7

Get raw version
php
  1. /**
  2.  * Implementation of hook_form_FORM_ID_alter
  3.  **/
  4. hook_form_FORM_ID_alter(&$form, &$form_state, $form_id){
  5.  
  6. $form['field_MY_FILE_FIELD_NAME']['und'][0]['#process'][] = 'my_processing_function';
  7.  
  8. }
  9.  
  10. /**
  11.  * My Ajax Upload File Processing Function
  12.  **/
  13. function my_processing_function($element, &$form_state){
  14.  
  15. // check to see if it's submitted and if there's a file
  16. if ($form_state['submitted'] && $element['#value']['fid']){
  17.  
  18. // Do stuff to the file here
  19. $fid = $element['#value']['fid']
  20. }
  21.  
  22. // make sure we return the element when we're done
  23. return $element;
  24. }

How to Permanently Save File Upload of Type managed_file
Here is information on how to save a file that has been uploaded using a form and the type managed_file. The file is normally saved only temporarily and will be deleted automatically during CRON runs.

The form code looks something like this,

Get raw version
php
  1. function MYMODULE_form ($form, &$form_state){
  2. $form['MY_IMAGE_NAME'] = array(
  3. '#title' => t('My Image'),
  4. '#type' => 'managed_file',
  5. '#description' => t('Please upload an image!'),
  6. '#default_value' => variable_get('MY_IMAGE_NAME', ''),
  7. '#progress_indicator' => 'bar',
  8. '#upload_location' => 'public://',
  9. );
  10. }
  11.  
  12. function MYMODULE_form_submit ($form, &$form_state) {
  13. /*Update the status to permanent*/
  14. $form['MY_IMAGE_NAME']['#file']->status = FILE_STATUS_PERMANENT;
  15. /*Save the updated file information to the database*/
  16. file_save($form['MY_IMAGE_NAME']['#file']);
  17. }