There is a file upload provision in my custom plugin page.
On this, I'm saving the image details into post table as an attachment
post type and moving the uploaded file manually. Here is my code:
$wp_upload_dir = wp_upload_dir();
$filetype = $_FILES['img_name'];
$filename = $filetype['name'];
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $filename);
move_uploaded_file($_FILES['test_upload_pdf']['tmp_name'],$wp_upload_dir['path'].'/'.$_FILES['test_upload_pdf']['name']);
Is there any alternative way for move_uploaded_file()
in WordPress to get the different dimension of the given image automatically?
wp_generate_attachment_metadata()
andwp_update_attachment_metadata()
afterwp_insert_attachment()
. See the first example here for details.$absFilename = $wp_upload_dir['path'].'/'.$filename; $attach_data = wp_generate_attachment_metadata( $attach_id, $absFilename); wp_update_attachment_metadata( $attach_id, $attach_data );