Media_Handle_Upload失败,重力表单已提交图像

时间:2017-05-17 作者:explorier

我在最近维护的一个网站上,这个功能已经停止工作了,我不知道确切的原因。第一个media\\u handle\\u upload功能失败,出现以下错误:

Array([error] => Specified file failed upload test.
尝试使用此代码而不是media\\u handle\\u upload,https://gist.github.com/hissy/7352933 , 但在使用gist中的代码时,我发现了一个错误:

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
这是我的功能。它连接到一个重力表单表单,该表单接受用户提交的照片。我想在媒体库中自动填充这些内容。它过去很管用,我不知道是什么破坏了它,可能是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 );

    }
}
非常感谢您在这方面的帮助或指导。如果需要,我也可以提供更清晰的信息或代码。谢谢

2 个回复
SO网友:cybmeta

该错误很可能是由于从wp admin错误加载所需文件所致;请注意,在您的代码中,仅当特定输入包含数据时,才会加载这些文件(input_13) 但并非所有情况下都需要这些文件。

您可以这样做:

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

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

    // ....
}
所以,如果$_FILES[\'input_13\'][\'name\'] 为空,则需要来自wp admin的这些文件的其余代码将失败。

尝试使用更合适的逻辑包含所需的文件。

SO网友:explorier

我从来没有弄明白为什么我的file\\u get\\u内容无法访问/tmp,但我找到了一个解决方法。我使用了gform\\u after\\u提交操作,并从gravity forms uploads目录中提取了该文件。使用media\\u handle\\u sideload,我能够将其导入媒体库并应用必要的元数据。

结束