1

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!

closed as off-topic by Christine Cooper, Mark Kaplun, Nathan Johnson, Michael, Jack Johansson Mar 13 '18 at 22:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Welcome to the WordPress StackExchange! Usually, questions exclusively regarding 3rd party plugins is considered to be off-topic and better asked using their respective support methods. Please take the tour and visit our help center to learn more. Check out the Contact Form 7 Support Forum. – Howdy_McGee Mar 12 '18 at 16:00
  • there has to be something else going on, as Howdy_McGee says, it is probably best to ask CF7 support forum. – Mark Kaplun Mar 12 '18 at 16:21
  • btw the error seems to indicate that the file you try to process was not uploaded at that session. – Mark Kaplun Mar 12 '18 at 16:23
  • Ok thanks guy, I wasn't sure the error was due to CF7, will ask there. I'll try to look up what you said about sessions @MarkKaplun, thanks! – nyy Mar 12 '18 at 17:14

Browse other questions tagged or ask your own question.