wrong with wp_generate_attachment_metadata()

It works (upload , send to db) but

wp_generate_attachment_metadata() returns bad letters whene file uploaded !

Read More

some of the wrong charechters :

����JFIF��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 90 ��C     ��C       ����"��   ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������    ���w!1AQaq"2�B����  #3R�br� $4�

the code :

if (isset($_FILES['ed_header_logo'] ) && !empty($_FILES['ed_header_logo']['name']) )
{
    $filename = $_FILES['ed_header_logo']['name'];
    $wp_filetype = wp_check_filetype( basename($filename), null );
    $wp_upload_dir = wp_upload_dir();

    move_uploaded_file( $_FILES['ed_header_logo']['tmp_name'], $wp_upload_dir['path']  . '/' . $filename );

    $url = $wp_upload_dir['url'] . '/' . basename( $filename );

    $attachment = array(
        'guid' => $url, 
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace( '/.[^.]+$/', '', basename( $filename ) ),
        'post_content' => 'weblogo',
        'post_status' => 'inherit'
    );


    $attach_id = wp_insert_attachment( $attachment, $url, 37 );
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $url );
    wp_update_attachment_metadata( $attach_id, $attach_data );  

}

Related posts

1 comment

  1. try this:

    if (isset($_FILES['ed_header_logo'] ) && !empty($_FILES['ed_header_logo']['name']) )
    
        {  
    
        $uploadedfile = $_FILES['ed_header_logo'];
    
        $upload_name = $_FILES['ed_header_logoe']['name'];
    
        $uploads = wp_upload_dir();
        $filepath = $uploads['path']."/$upload_name";
    
    if ( ! function_exists( 'wp_handle_upload' ) )
        {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        }
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
    
        $upload_overrides = array( 'test_form' => false );
        //$attach_id = media_handle_upload( $file, $new_post );
        $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
        //print_r($movefile);
        if ( $movefile && !isset( $movefile['error'] ) ) {
    
        $file = $movefile['file'];
        $url = $movefile['url'];
        $type = $movefile['type'];
    
        //media_handle_upload( $file_handler, 0 );
        $attachment = array(
        'post_mime_type' => $type ,
        'post_title' => $upload_name,
        'post_content' => 'Image for '.$upload_name,
        'post_status' => 'inherit'
        );
    
        $attach_id=wp_insert_attachment( $attachment, $file, 0);
        $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
        wp_update_attachment_metadata( $attach_id, $attach_data );
    
        }
    

Comments are closed.