0

Been stuck on this for a while now -- This function stopped working on a site I maintain recently, and I'm not sure exactly why. It is failing at the first media_handle_upload function and gives me this error:

Array([error] => Specified file failed upload test.

Tried using this code instead of media_handle_upload, https://gist.github.com/hissy/7352933 , but in with the code from the gist I got this error:

017/05/17 05:45:59 [error] 2066#2066: *10499 FastCGI sent in stderr: "PHP message: PHP Warning:  file_get_contents(/tmp/php8mjcrC): failed to open stream: No such file or directory in /srv/www/test.dev/current/web/app/themes/test/functions.php on line 1083

Here is my function. It is hooking into a Gravity Forms form that accepts user submitted photos. I want these to be auto-populated in the media library. It used to work, I'm not sure what broke it, potentially something in WordPress v4.7

add_action("gform_pre_submission_16", "post_submission");

function post_submission(){
    $body = 'From: '.$_POST['input_2'];
    $body .= '<br/>Email: '.$_POST['input_3'];
    $body .= '<br/>Description: '.$_POST['input_5'];

    $mypostID = 0; // change it to your desired post id

    $photo_description = $_POST['input_5'];

    $photo_credit = $_POST['input_2'];

    if( !empty($_FILES['input_13']['name'])) {
        require_once(ABSPATH . 'wp-admin/includes/file.php');

        require_once(ABSPATH . 'wp-admin/includes/image.php');

        require_once(ABSPATH . 'wp-admin/includes/media.php');
        $attach_id = media_handle_upload('input_13', $mypostID);
        $strain_cat_id = $_POST['input_20'];
        $cat_id = $_POST['input_21'];

        wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, 170, 'media_category', true );
        $my_post = array(
            'ID'           => $attach_id,
            'post_content' => $photo_description
            );

        wp_update_post( $my_post );

        update_post_meta( $attach_id, 'photo_credit', $photo_credit );
        $body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';
    }


    if( !empty($_FILES['input_14']['name']) ) {

        $attach_id = media_handle_upload('input_14', $mypostID);

        $strain_cat_id = $_POST['input_22'];

        $cat_id = $_POST['input_26'];

        wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, 170, 'media_category', true );

        $my_post = array(
            'ID'           => $attach_id,
            'post_content' => $photo_description
            );

        wp_update_post( $my_post );

        update_post_meta( $attach_id, 'photo_credit', $photo_credit );

        $body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';

    }
    if( !empty($_FILES['input_15']['name']) ) {

        $attach_id = media_handle_upload('input_15', $mypostID);

        $strain_cat_id = $_POST['input_23'];

        $cat_id = $_POST['input_27'];

        wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, 170, 'media_category', true );

        $my_post = array(
            'ID'           => $attach_id,
            'post_content' => $photo_description
            );

        wp_update_post( $my_post );

        update_post_meta( $attach_id, 'photo_credit', $photo_credit );

        $body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';

    }
    if( !empty($_FILES['input_16']['name']) ) {

        $attach_id = media_handle_upload('input_16', $mypostID);

        $strain_cat_id = $_POST['input_24'];

        $cat_id = $_POST['input_28'];

        wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, 170, 'media_category', true );

        $my_post = array(
            'ID'           => $attach_id,
            'post_content' => $photo_description
            );

        wp_update_post( $my_post );

        update_post_meta( $attach_id, 'photo_credit', $photo_credit );

        $body .= '<br/><a href="http://darkheartnursery.com/wp-admin/upload.php?item='.$attach_id.'">View Photo for '.$strain_cat_id.'</a>';

    }
    if( !empty($_FILES['input_17']['name']) ) {

        $attach_id = media_handle_upload('input_17', $mypostID);

        $strain_cat_id = $_POST['input_25'];

        $cat_id = $_POST['input_29'];

        wp_set_object_terms( $attach_id, $strain_cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, $cat_id, 'media_category', true );
        wp_set_object_terms( $attach_id, 170, 'media_category', true );

        $my_post = array(
            'ID'           => $attach_id,
            'post_content' => $photo_description
            );

        wp_update_post( $my_post );

        update_post_meta( $attach_id, 'photo_credit', $photo_credit );

    }
}

Really appreciate any help or guidance on this. I can provide more clarity or code if needed as well. Thanks!

0

2 Answers 2

0

That error is very probabbly due to incorrect loading of the required files from wp-admin; note how in your code those files are loaded only when a scpecific input has data (input_13) but not in every case you need those files.

You do this:

if( !empty($_FILES['input_13']['name'])) {
    require_once(ABSPATH . 'wp-admin/includes/file.php');

    require_once(ABSPATH . 'wp-admin/includes/image.php');

    // ....
}

So, if $_FILES['input_13']['name'] is empty, the rest of code that needs those files from wp-admin will fail.

Try to include the required files with a more appropiate logic.

2
  • This isn't the issue. That code was definitely executing correctly. To make sure I took out the if statement and left the code inside, and still got this error when I print_r my variable $attach_id: Array([error] => Specified file failed upload test.
    – explorier
    May 17, 2017 at 19:00
  • Can you post the HTML form you are using to upload the files?
    – cybmeta
    May 18, 2017 at 5:02
0

I never figured out why I file_get_contents couldn't access /tmp, however I figured out a workaround. I used the gform_after_submission action, and pulled the file from the gravity forms uploads directory. Using media_handle_sideload I was able to import it into the media library and apply the necessary metadata.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.