php - wrong with wp_generate_attachment_metadata() -
it works (upload , send db) but
wp_generate_attachment_metadata()
returns bad letters whene file uploaded !
some of 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 ); }
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 '.$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
Post a Comment