WP_HANDLE_UPLOAD错误“指定的文件未通过上载测试”,但仍创建附件?

时间:2011-07-18 作者:daveaspinall

图像上传自定义元框有点问题。所发生的情况是,上载的图像被创建为附件,但wp\\u handle\\u上载似乎会回退错误“指定文件上载测试失败”,而不是更新我的帖子meta?虽然附件创建时没有问题,但这似乎很奇怪?

代码如下:

// Make the forms multipart
add_action(\'post_edit_form_tag\', \'post_edit_form_tag\');
function post_edit_form_tag() {
   echo \' enctype="multipart/form-data"\';
}

/* Setup custom meta boxes for posts */
$new_meta_boxes =
    array(
    "xxxx_kw1" => array(
        "name" => "xxxx_kw1",
        "std" => "",
        "title" => "Keyword 1",
        "type" => "text"
        ),
    "xxxx_url1" => array(
        "name" => "xxxx_url1",
        "std" => "",
        "title" => "URL 1",
        "type" => "text"
        ),
    "xxxx_kw2" => array(
        "name" => "xxxx_kw2",
        "std" => "",
        "title" => "Keyword 2",
        "type" => "text"
        ),
    "xxxx_url2" => array(
        "name" => "xxxx_url2",
        "std" => "",
        "title" => "URL 2",
        "type" => "text"
        ),
    "xxxx_kw3" => array(
        "name" => "xxxx_kw3",
        "std" => "",
        "title" => "Keyword 3",
        "type" => "text"
        ),
    "xxxx_url3" => array(
        "name" => "xxxx_url3",
        "std" => "",
        "title" => "URL 3",
        "type" => "text"
        ),
    "xxxx_image" => array(
        "name" => "xxxx_image",
        "std" => "",
        "title" => "Image Upload",
        "type" => "file"
        )

);

function new_meta_boxes() { 
    global $post, $new_meta_boxes;

    foreach($new_meta_boxes as $meta_box) {
        $meta_box_value = get_post_meta($post->ID, $meta_box[\'name\'], true);

        if($meta_box[\'type\'] == \'file\') { $meta_box_value = get_post_meta($post->ID,\'xxxx_attached_image\', true); }

        if($meta_box_value == "")
        $meta_box_value = $meta_box[\'std\']; 

        echo\'<input type="hidden" name="\'.$meta_box[\'name\'].\'_noncename" id="\'.$meta_box[\'name\'].\'_noncename" value="\'.wp_create_nonce( plugin_basename(__FILE__) ).\'" />\';

        echo\'<p><label for="\'.$meta_box[\'name\'].\'">\'.$meta_box[\'title\'].\'</label></p>\';

        echo\'<input type="\'.$meta_box[\'type\'].\'" name="\'.$meta_box[\'name\'].\'" value="\'.$meta_box_value.\'" size="55" />\';
    }
}

function create_meta_box() {
    global $theme_name;
    if ( function_exists(\'add_meta_box\') ) {
        add_meta_box( \'new-meta-boxes\', \'PR xxxx Paid Information\', \'new_meta_boxes\', \'post\', \'normal\', \'high\' );
    }
}

function save_postdata( $post_id ) {
    global $post, $new_meta_boxes;

    $_POST = array_map(\'mysql_real_escape_string\', $_POST);

    foreach($new_meta_boxes as $meta_box) {
        // Verify
        if ( !wp_verify_nonce( $_POST[$meta_box[\'name\'].\'_noncename\'], plugin_basename(__FILE__) )) {
            return $post_id;
        }

        if ( !current_user_can( \'edit_post\', $post_id ))
            return $post_id;

        if($meta_box[\'type\'] == \'file\') : // if data is a file we\'ll need to treat if differently...

            $data = $_FILES[$meta_box[\'name\']];

            // If the upload field has a file in it
            if(isset($data) && ($data[\'size\'] > 0)) {

                // Get the type of the uploaded file. This is returned as "type/extension"
                $arr_file_type = wp_check_filetype(basename($data[\'name\']));
                $uploaded_file_type = $arr_file_type[\'type\'];

                // Set an array containing a list of acceptable formats
                $allowed_file_types = array(\'image/jpg\',\'image/jpeg\',\'image/gif\',\'image/png\');

                // If the uploaded file is the right format
                if(in_array($uploaded_file_type, $allowed_file_types)) {

                    // Options array for the wp_handle_upload function. \'test_upload\' => false
                    $upload_overrides = array( \'test_form\' => false ); 

                    // Handle the upload using WP\'s wp_handle_upload function. Takes the posted file and an options array
                    $uploaded_file = wp_handle_upload($data, $upload_overrides);

                    // If the wp_handle_upload call returned a local path for the image
                    if(isset($uploaded_file[\'file\'])) {

                        // The wp_insert_attachment function needs the literal system path, which was passed back from wp_handle_upload
                        $location = $uploaded_file[\'file\'];

                        // Generate a title for the image that\'ll be used in the media library
                        $file_title = $uploaded_file[\'file\'];

                        // Set up options array to add this file as an attachment
                        $attachment = array(
                            \'post_mime_type\' => $uploaded_file_type,
                            \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($file_title)),
                            \'post_content\' => \'\',
                            \'post_status\' => \'inherit\',
                            \'post_parent\' => $post_id,
                            \'guid\' => $uploaded_file[\'url\']
                        );

                        // Run the wp_insert_attachment function. This adds the file to the media library and generates the thumbnails. If you wanted to attach this image to a post, you could pass the post id as a third param and it\'d magically happen.
                        $attach_id = wp_insert_attachment( $attachment, $location, $post_id );
                        require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
                        $attach_data = wp_generate_attachment_metadata( $attach_id, $location );
                        wp_update_attachment_metadata($attach_id,  $attach_data);

                        // Before we update the post meta, trash any previously uploaded image for this post.
                        // You might not want this behavior, depending on how you\'re using the uploaded images.
                        $existing_uploaded_image = (int) get_post_meta($post_id,\'xxxx_attached_image\', true);
                        if(is_numeric($existing_uploaded_image)) {
                            wp_delete_attachment($existing_uploaded_image);
                        }

                        // Now, update the post meta to associate the new image with the post
                        update_post_meta($post_id,\'xxxx_attached_image\',$attach_id);

                        // Set the feedback flag to false, since the upload was successful
                        $upload_feedback = false;


                    } else { // wp_handle_upload returned some kind of error. the return does contain error details, so you can use it here if you want.

                        $upload_feedback = \'There was a problem with your upload: \'.$uploaded_file[\'error\'];
                        update_post_meta($post_id,\'xxxx_attached_image\',$attach_id);

                    }

                } else { // wrong file type

                    $upload_feedback = \'Please upload only image files (jpg, gif or png).\';
                    update_post_meta($post_id,\'xxxx_attached_image\',$attach_id);

                }

            } else { // No file was passed

                $upload_feedback = false;

            }

            // Update the post meta with any feedback
            update_post_meta($post_id,\'xxxx_attached_image_upload_feedback\',$upload_feedback);

        else : // valid the input as normal

            $data = $_POST[$meta_box[\'name\']];

            if(get_post_meta($post_id, $meta_box[\'name\']) == "")
                add_post_meta($post_id, $meta_box[\'name\'], $data, true);
            elseif($data != get_post_meta($post_id, $meta_box[\'name\'], true))
                update_post_meta($post_id, $meta_box[\'name\'], $data);
            elseif($data == "")
                delete_post_meta($post_id, $meta_box[\'name\'], get_post_meta($post_id, $meta_box[\'name\'], true));

        endif;

    }
}

add_action(\'admin_menu\', \'create_meta_box\');
add_action(\'save_post\', \'save_postdata\');
如果有人能帮忙,我会竖起大拇指,送一杯啤酒!:-)

干杯

戴夫

1 个回复
SO网友:Anatol

尝试使用media_handle_upload() 相反,为我解决了一个类似的问题。

结束

相关推荐

在另一个metabox中使用WPAlChemy metabox值

我正在构建一个非常复杂的自定义帖子类型结构,我需要一些关于我的元盒的帮助。我想做什么:谢谢have_fields_and_multi() 功能,用户在简单的文本输入字段中输入数据(使用“添加新”按钮)应使用以前文本输入的值来构建select 另一个元框中的下拉列表</为了简单起见,这里有一个模型(也附在这篇文章后面):http://idzr.org/0c95我有第一部分工作,很简单。但我不知道如何让第二部分发挥作用。如果我使用while($mb->have_fields_and_multi(\