• Hi,

    I’m starting to gain back progrematic access to uploaded attachments, with version 5.4.

    The key here is that now an uploaded attachment file is in an array. This requires “[0]” suffix retrieving data.

    Sadly, the above did not fully serve as a solution.

    I am starting to see a possible solution, reading the code of the Contact Form 7 Database Addon – CFDB7 plugin. This plugin is already adjusted to fetching files, and is competable with CF7 new data structure.

    It’s contact-form-cfdb-7.php code contains the following code:

    function cfdb7_before_send_mail( $form_tag ) {
    
        global $wpdb;
        $cfdb          = apply_filters( 'cfdb7_database', $wpdb );
        $table_name    = $cfdb->prefix.'db7_forms';
        $upload_dir    = wp_upload_dir();
        $cfdb7_dirname = $upload_dir['basedir'].'/cfdb7_uploads';
        $time_now      = time();
    
        $submission   = WPCF7_Submission::get_instance();
        $contact_form = $submission->get_contact_form();
        $tags_names   = array();
        $strict_keys  = apply_filters('cfdb7_strict_keys', false);  
    
        if ( $submission ) {
    
            $allowed_tags = array();
    
            if( $strict_keys ){
                $tags  = $contact_form->scan_form_tags();
                foreach( $tags as $tag ){
                    if( ! empty($tag->name) ) $tags_names[] = $tag->name;
                }
                $allowed_tags = $tags_names;
            }
    
            $not_allowed_tags = apply_filters( 'cfdb7_not_allowed_tags', array( 'g-recaptcha-response' ) );
            $allowed_tags     = apply_filters( 'cfdb7_allowed_tags', $allowed_tags );
            $data             = $submission->get_posted_data();
            $files            = $submission->uploaded_files();
            $uploaded_files   = array();
    
            foreach ($_FILES as $file_key => $file) {
                array_push($uploaded_files, $file_key);
            }
            foreach ($files as $file_key => $file) {
                $file = is_array( $file ) ? reset( $file ) : $file;
                if( empty($file) ) continue;
                copy($file, $cfdb7_dirname.'/'.$time_now.'-'.$file_key.'-'.basename($file));
            }
    
            $form_data   = array();
    
            $form_data['cfdb7_status'] = 'unread';
            foreach ($data as $key => $d) {
                
                if( $strict_keys && !in_array($key, $allowed_tags) ) continue;
    
                if ( !in_array($key, $not_allowed_tags ) && !in_array($key, $uploaded_files )  ) {
    
                    $tmpD = $d;
    
                    if ( ! is_array($d) ){
                        $bl   = array('\"',"\'",'/','\\','"',"'");
                        $wl   = array('"',''','/', '\','"',''');
                        $tmpD = str_replace($bl, $wl, $tmpD );
                    }
    
                    $form_data[$key] = $tmpD;
                }
                if ( in_array($key, $uploaded_files ) ) {
                    $file = is_array( $files[ $key ] ) ? reset( $files[ $key ] ) : $files[ $key ];
                    $file_name = empty( $file ) ? '' : $time_now.'-'.$key.'-'.basename( $file ); 
                    $form_data[$key.'cfdb7_file'] = $file_name;
                }
            }
    
            /* cfdb7 before save data. */
            $form_data = apply_filters('cfdb7_before_save_data', $form_data);
    
            do_action( 'cfdb7_before_save', $form_data );
    
            $form_post_id = $form_tag->id();
            $form_value   = serialize( $form_data );
            $form_date    = current_time('Y-m-d H:i:s');
    
            $cfdb->insert( $table_name, array(
                'form_post_id' => $form_post_id,
                'form_value'   => $form_value,
                'form_date'    => $form_date
            ) );
    
            /* cfdb7 after save data */
            $insert_id = $cfdb->insert_id;
            do_action( 'cfdb7_after_save_data', $insert_id );
        }
    
    }
    
    add_action( 'wpcf7_before_send_mail', 'cfdb7_before_send_mail' );

    I recommend focusing at this part of the code:

    $data             = $submission->get_posted_data();
            $files            = $submission->uploaded_files();
            $uploaded_files   = array();
    
            foreach ($_FILES as $file_key => $file) {
                array_push($uploaded_files, $file_key);
            }
            foreach ($files as $file_key => $file) {
                $file = is_array( $file ) ? reset( $file ) : $file;
                if( empty($file) ) continue;
                copy($file, $cfdb7_dirname.'/'.$time_now.'-'.$file_key.'-'.basename($file));
            }
  • The topic ‘Access to Uploaded Attachments, as of Version 5.4’ is closed to new replies.