如何在服务器目录联系表单上保留上传的文件或附件7

Ran*_*ngo 1 php wordpress

我正在使用联系表单7的博客上工作.但我需要将这些信息保存到数据库中,并希望在用户配置文件中显示附件.由于cf7在wpcf_upload文件夹中上传图像或附件.我可以通过以下方式保存自定义表中的所有数据:

add_action( 'wpcf7_before_send_mail', 'save_application_form');
Run Code Online (Sandbox Code Playgroud)

所以首先我必须在服务器上永久保存cf7的图像或附件.

那么请告诉我该怎么做?

谢谢

小智 8

这是一个链接.

我简化了上面链接的代码.也许它有帮助:

 function cf7_create_post($data) {
    //In case you wanna check for a especific form
    /* $form_id = $data->id;
      if ($form_id != '1234')
      return;/* */

    //Get the current posted form instance
    $form_to_DB = WPCF7_Submission::get_instance();
    if ($form_to_DB) {
        $formData = $form_to_DB->get_posted_data(); // Get all data from the posted form
        $uploaded_files = $form_to_DB->uploaded_files(); // this allows you access to the upload file in the temp location
    }

    // Let's insert the new post first to get the ID.
    $newpostid = wp_insert_post(array(
        'post_status' => 'draft',
        'post_title' => "Your new Post Title", // Can be  $formData['YOUR-CF7-FIELD-NAME']
        'post_type' => "post", //Post Type
        'post_content' => "Your new Post Content", // Can be  $formData['YOUR-CF7-FIELD-NAME']
    ));


    // We need to get the CF7 field name from FILE
    $cf7_file_field_name = 'uploadyourfile'; // [file uploadyourfile]

    //Do the magic the same as the refer link above
    $image_name = $formData[$cf7_file_field_name];
    $image_location = $uploaded_files[$cf7_file_field_name];
    $image_content = file_get_contents($image_location);
    $wud = wp_upload_dir();
    $upload = wp_upload_bits($image_name, null, $image_content);
    $chemin_final = $upload['url'];
    $filename = $upload['file'];
    if ($filename > '') {
        require_once(ABSPATH . 'wp-admin/includes/admin.php');
        $wp_filetype = wp_check_filetype(basename($filename), null);
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit'
        );
        $attach_id = wp_insert_attachment($attachment, $filename, $newpostid);
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
        wp_update_attachment_metadata($attach_id, $attach_data);
        //Define the new Thumbnail can be also a ACF field
        update_post_meta($newpostid, "_thumbnail_id", $attach_id);
    }
}
add_action('wpcf7_before_send_mail', 'cf7_create_post',1);//added to functions.php
Run Code Online (Sandbox Code Playgroud)

所有学分都转到上面的链接,我只是改了一下.;)