cf_handle_file_upload

Handle uploading of files from file fields.

Description

(bool|mixed) cf_handle_file_upload( $entry, $field, $form ); 

Returns (bool|mixed)

Parameters (3)

0. $entry
The entry.
1. $field
The field.
2. $form
The form.

Usage

  1. if ( !function_exists( 'cf_handle_file_upload' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'caldera-forms/includes/field_processors.php'; 
  3.  
  4. // The entry. 
  5. $entry = null; 
  6.  
  7. // The field. 
  8. $field = null; 
  9.  
  10. // The form. 
  11. $form = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = cf_handle_file_upload($entry, $field, $form); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/field_processors.php  
  1. function cf_handle_file_upload( $entry, $field, $form ) { 
  2. if( ! Caldera_Forms_Field_Util::is_file_field( $field, $form ) ) { 
  3. return false; 
  4.  
  5. // check transdata if string based entry 
  6. if( is_string( $entry ) ) { 
  7. $transdata = Caldera_Forms_Transient::get_transient( $entry ); 
  8.  
  9. if( !empty( $transdata ) ) { 
  10.  
  11. return $transdata; 
  12.  
  13.  
  14. if( isset($_POST[ '_cf_frm_edt' ] ) ) { 
  15. if ( ! isset( $_FILES ) 
  16. || ( isset( $_FILES[ $field[ 'ID' ] ][ 'size' ][0] ) && 0 == $_FILES[ $field[ 'ID' ] ][ 'size' ][0] ) 
  17. || ( isset( $_FILES[ $field[ 'ID' ] ][ 'size' ] ) && 0 == $_FILES[ $field[ 'ID' ] ][ 'size' ] ) 
  18. ) { 
  19. $entry = Caldera_Forms::get_field_data( $field[ 'ID' ], $form, absint( $_POST[ '_cf_frm_edt' ] ) ); 
  20.  
  21. return $entry; 
  22. $required = false; 
  23. if ( isset( $field[ 'required' ] ) && $field[ 'required' ] ) { 
  24. $required = true; 
  25. if(!empty($_FILES[$field['ID']]['size'])) { 
  26.  
  27. // buildwpallowed types 
  28. $allowed = get_allowed_mime_types(); 
  29. $wp_allowed = array(); 
  30. foreach( $allowed as $ext=>$mime ) { 
  31. $exts = explode('|', $ext ); 
  32. foreach( $exts as $ext ) { 
  33. $wp_allowed[ strtolower( $ext ) ] = true; 
  34.  
  35. // check if user set allowed types 
  36. if(!empty($field['config']['allowed'])) { 
  37. $allowed = array_map('trim', explode(', ', trim( $field['config']['allowed'] ) ) ); 
  38. $field['config']['allowed'] = array(); 
  39. foreach( $allowed as $ext ) { 
  40. $ext = strtolower( trim( $ext, '.' ) ); 
  41. if( in_array($ext, $wp_allowed ) ) { 
  42. $field['config']['allowed'][ $ext ] = true; 
  43. }else{ 
  44. //set allowed to only whatwpallows 
  45. $field['config']['allowed'] = $wp_allowed; 
  46.  
  47. // check each file now 
  48. foreach( (array) $_FILES[$field['ID']]['name'] as $file_name ) { 
  49. if( empty( $file_name ) ) { 
  50. return $entry; 
  51. $filetype = wp_check_filetype( basename( $file_name ), null ); 
  52. if( empty( $field['config']['allowed'][ strtolower( $filetype['ext'] ) ] ) ) { 
  53. return new WP_Error( 'fail', __('This file type is not allowed. Please try another.', 'caldera-forms') ); 
  54.  
  55. if ( ! function_exists( 'wp_handle_upload' ) ) { 
  56. require_once( ABSPATH . 'wp-admin/includes/file.php' ); 
  57.  
  58. $files = array(); 
  59. foreach( (array) $_FILES[$field['ID']] as $file_key=>$file_parts ) { 
  60. foreach( (array) $file_parts as $part_index=>$part_value ) { 
  61. $files[ $part_index ][ $file_key ] = $part_value; 
  62.  
  63. $uploads = array(); 
  64. foreach( $files as $file ) { 
  65. if( ! $required && 0 == $file[ 'size' ] ) { 
  66. continue; 
  67.  
  68. if( ! Caldera_Forms_Files::is_private( $field ) ) { 
  69. $upload_args = array( 
  70. 'private' => false,  
  71. 'field_id' => $field['ID'],  
  72. 'form_id' => $form['ID'] 
  73. ); 
  74. }else{ 
  75. $upload_args = array( 
  76. 'private' => true,  
  77. 'field_id' => $field['ID'],  
  78. 'form_id' => $form['ID'] 
  79. ); 
  80.  
  81. $uploader = Caldera_Forms_Files::get_upload_handler( $form, $field ); 
  82. if( is_callable( $uploader) ) { 
  83. $upload = call_user_func( $uploader, $file, $upload_args ); 
  84. }else{ 
  85. return new WP_Error( 'invalid-upload-handler', __( sprintf( 'Invalid file upload handler. See %s', ' https://calderaforms.com/doc/alternative-file-upload-directory/'), 'caldera-forms') ); 
  86.  
  87. if( !empty( $upload[error] ) ) { 
  88. return new WP_Error( 'fail', $upload[error] ); 
  89. $uploads[] = $upload['url']; 
  90. // check media handler 
  91. if( !empty( $field['config']['media_lib'] ) ) { 
  92. Caldera_Forms_Files::add_to_media_library( $upload ); 
  93.  
  94. if( count( $uploads ) > 1 ) { 
  95. return $uploads; 
  96.  
  97. if( empty( $uploads ) ) { 
  98. return array(); 
  99.  
  100. return $uploads[0]; 
  101. }else{ 
  102. // for multiples 
  103. if( is_array( $entry ) ) { 
  104. foreach( $entry as $index => $line ) { 
  105. if( !filter_var( $line, FILTER_VALIDATE_URL ) ) { 
  106. unset( $entry[ $index ] ); 
  107. return $entry; 
  108. }else{ 
  109. if( filter_var( $entry, FILTER_VALIDATE_URL ) ) { 
  110. return $entry; 
  111.  
  112.