CF7 upload file failing

So I’ve been trying to solve this for days now but just don’t get it.
I have a form on a WordPress site (Contact form 7) where a user can fill in some data and submit it, this will then create a post in a custom post type. All of this works fine. The problem is by the end of the form there’s a file attachment field. And the files aren’t ending up in my WordPress library nor is it even ending up on the server.

My form looks like this:

[select* pri_act_type include_blank “Value1” “Value2” “Value3” “Value4”]

[text* pri_act_name placeholder “Your name”]

[email* pri_act_email placeholder “Your email address”]

… A bunch of fields here …

[file image limit:10000000 filetypes:jpg|jpeg|png|gif]

[submit “Send”]

The function that hooks into the submission and creates the post and finally tries to upload the file looks like this:

function action_wpcf7_before_send_mail( $contact_form ) {

$wpcf = WPCF7_ContactForm::get_current();
if ($wpcf->id == 120) {
$wpcf->skip_mail = true;

***….. <A bunch of code that creates the post> ……***

// Upload file
if ( ! function_exists( ‘wp_handle_upload’ ) ) {
require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
}

$uploadedfile = $_FILES[‘image’];

$upload_overrides = array( ‘test_form’ => false );

$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

if ( $movefile && ! isset( $movefile[‘error’] ) ) {
$status_message = “File is valid, and was successfully uploaded.n”;
$vatt = var_dump( $movefile );
} else {
/**
* Error generated by _wp_handle_upload()
* @see _wp_handle_upload() in wp-admin/includes/file.php
*/
$status_message = $movefile[‘error’];
}
}
};
add_action( ‘wpcf7_before_send_mail’, ‘action_wpcf7_before_send_mail’, 10, 1 );

Now the problem is, this part: if ( $movefile && ! isset( $movefile[‘error’] ) ) always fails. The $movefile[‘error’]; is always:

Specified file failed upload test.

When I debug it on my local server I come all the way down to this:

$test_uploaded_file = ‘wp_handle_upload’ === $action ? @ is_uploaded_file( $file[‘tmp_name’] ) : @ is_file( $file[‘tmp_name’] );
if ( ! $test_uploaded_file ) {
return call_user_func_array( $upload_error_handler, array( &$file, __( ‘Specified file failed upload test.’ ) ) );
}

This is where the error is thrown. I’m pretty sure the problem is that is_uploaded_file() fails. This is what’s given to it from $file[‘tmp_name’]:

Array
(
[name] => Screen Shot 2018-03-12 at 12.42.43.png
[type] => image/png
[tmp_name] => /Applications/MAMP/tmp/php/php9ej3tB
[error] => 0
[size] => 33177
)

So when I look it up in the PHP docs it says:

The is_uploaded_file() function checks whether the specified file is
uploaded via HTTP POST.

This function returns TRUE if the file is uploaded via HTTP POST.

But I’m not sure what this means. How is it NOT uploaded via HTTP POST? Or am I missing something obvious here? I’ve tried uploading files via the admin page (Media library) and tried running this on a proper server and not just my local env. But nothing works. Help!

Read more here:: CF7 upload file failing

Leave a Reply

Your email address will not be published. Required fields are marked *