media_handle_upload weird thing

I am using a front end script to upload images , all works fine but recently i noticed the uploaded images are not saved in proper directory. My settings are to Organize my uploads into month- and year-based folders. my folders are 2011->03,04,05 (march , April , may). The problem is that if i upload a file today it saves in folder 03(March). If i use wp_handle_upload then the image is saved in proper folder 05 (May) , but then the image is not shown in media library and the different sizes are not made.

Code i am using is

$image = media_handle_upload('async-upload', '');

1 Answer
1

Look into the first lines of this function:

function media_handle_upload(
    $file_id, 
    $post_id, 
    $post_data = array(), 
    $overrides = array( 'test_form' => false )
) 
{

    $time = current_time('mysql');
    if ( $post = get_post($post_id) ) {
        if ( substr( $post->post_date, 0, 4 ) > 0 )
            $time = $post->post_date;
    }

    $name = $_FILES[$file_id]['name'];
    $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);

The time is taken from the post date. You could bypass media_handle_upload() and use wp_handle_upload() directly.

Update

I don’t have the time to write a fully working standalone example, but here is an excerpt from my theme options class. It handles uploads, generates different images sizes and an attachment id. It is called by the general save function and handles multiple uploaded files in one rush.

/**
 * Saves uploaded files in media library and the corresponding id in option field.
 *
 * @return void
 */
protected function handle_uploads()
{
    if ( ! isset ( $_FILES ) or empty ( $_FILES ) )
    {
        return;
    }

    foreach ( $_FILES as $file_key => $file_arr )
    {
        // Some bogus upload.
        if ( ! isset ( $this->fields[$file_key] )
            or empty ( $file_arr['type'] )
        )
        {
            continue;
        }

        if ( ! $this->is_allowed_mime( $file_key, $file_arr ) )
        {
            set_theme_mod( $file_key . '_error', 'wrong mime type' );
            continue;
        }

        // The file is allowed, no error until now and the type is correct.
        $uploaded_file = wp_handle_upload(
            $file_arr
        ,   array( 'test_form' => FALSE )
        );

        // error
        if ( isset ( $uploaded_file['error'] ) )
        {
            set_theme_mod( $file_key . '_error', $uploaded_file['error'] );
            continue;
        }

        // add the file to the media library

        // Set up options array to add this file as an attachment
        $attachment = array(
            'post_mime_type' => $uploaded_file['type']
        ,   'post_title'     => $this->get_media_name(
                                    $file_key, $uploaded_file['file']
                                )
        );

        // Adds the file to the media library and generates the thumbnails.
        $attach_id = wp_insert_attachment(
            $attachment
        ,   $uploaded_file['file']
        );

        $this->create_upload_meta( $attach_id, $uploaded_file['file'] );

        // Update the theme mod.
        set_theme_mod( $file_key, $attach_id );
        remove_theme_mod( $file_key . '_error' );
    }
}

/**
 * Adds meta data to the uploaded file
 *
 * @param  int $attach_id
 * @param  string $file
 * @return void
 */
protected function create_upload_meta( $attach_id, $file )
{
    // Create meta data from EXIF fields.
    require_once ABSPATH . 'wp-admin/includes/image.php';
    $attach_data = wp_generate_attachment_metadata(
        $attach_id
    ,   $file
    );
    wp_update_attachment_metadata($attach_id,  $attach_data);
}

Some notes:

  • is_allowed_mime() checks for MIME types set in $fields which are set in my controller. The MIME type for a favicon for example is either image/x-icon or image/vnd.microsoft.icon.
  • get_media_name() may use a predefined name for the attachment (e.g. logo).
  • In your function you probably want to return $attach_id and use it in a post meta field or something similar.

Leave a Comment