You want media_handle_sideload()
Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
// Array similar to a $_FILES upload array.
$file_array = array(
'name' => 'filename.jpg',
'tmp_name' => 'path/to/filename.jpg',
);
// Post ID to attach upload to, 0 for none.
$post_id = 0;
$attachment_id = media_handle_sideload( $file_array, $post_id );
Related Posts:
- How do I detach images from posts?
- Replacing all attachment links in post with media file link
- Easy way to delete 70k posts and attached media?
- How to access featured image?
- Errors when uploading images in WP 3.5
- Set size for attachments
- Automatically attach image to post if they have the same title
- Remove Featured Image & All Media Uploaded to the Post
- Bulk Attach Or Insert Media To Post Without Opening Post Editor
- Broken images after importing posts and opening in Gutenberg
- Turn a URL into an Attachment / Post ID
- How to Fix HTTP Error When Uploading Images?
- How do you add thumbnail support for custom post types?
- ZIP up all images displayed in a [gallery] and offer as download link
- Delete Associated Media Upon Page Deletion
- Post featured image column on admin post list page
- Delete post with all files attached to it
- Is it possible to Schedule Attachments in WordPress?
- How can you change the ‘Insert into Post’ title in the media button?
- what types of order can I pass to get_posts() via orderby?
- Find the post an attachment is attached to
- Custom Field in Featured image for A particular post
- How to add/assign or change featured image in post programmatically?
- Use an attachment in multiple posts
- delete post also attachments
- Get post id by attachment id?
- How do I use element instead of tags in WordPress post content having webP support?
- How can I export WordPress posts’ attachments?
- How to remove images from showing in a post with the_content()?
- VT-resize image and display it [duplicate]
- upload featured image from front end using wordpress add media button
- How do I create Comma Separated list of attached image ids?
- Get post embedded image caption
- Posts Page Featured Image [closed]
- Showing Thumbnail from Previous and Next Posts
- How do I programmatically add an image to a post?
- How to Replace the WordPress Featured Image with a Video?
- Limit amount of results returned
- Delete Post From Front-End and attachment permanently
- Require a featured image to publish post
- post_parent is 0 when uploading new files
- Underscores.me retrieve next / previous post thumbnail in post_nav function
- How to Set Featured image In WordPress without Showing it into Post?
- How only display all post related to category
- How do I find which articles are missing a featured image in The WordPress database?
- Modify WP_Post before processing
- Show featured image next to post-teasers in Genesis Framework?
- Image still attached to post even after removing it as featured image
- Admin Notices don’t display on the admin screen for certain post
- What is the standard featured image size?
- Load post attached images on a single page site with fancybox
- Too many connections to server
- Insert woocommerce products programmatically with featured image and gallery
- Add the description field on the attached media
- How can I get the media inside a post?
- I have a site where attachments in the wp_posts table have guid values of guid, how did this happen?
- Get all attachment by post excluding featured and content inside
- Prevent Delete Attachment by URL or When Submit
- wp-cli post create & media import issues
- Posts and Attachments with “Published” status in Search
- Get attachments for posts that belongs to a specific category
- why are images not full size?
- Post body text occupying featured image space before it loads
- Removal of Media Gallery pictures
- How can you change all past posts’ images to link to none instead of the image file?
- Get Image tag from content of post
- How to get the last selected media alignment setting?
- Get all images in post and comments like Twitter before post title
- Can’t update old posts 3.5.2
- WordPress Automatic Filename Changer
- Issue where WP Featured Image will not display
- How would one change the default url structure of attachments?
- How to check if single.php has already called the_post_thumbnail function
- Getting only the URL of the post thumbnail
- Returning data instead of echoing/printing
- Get Post’s first image using Short-code
- related author post thumbnail shows post thumbnail
- Admin – no Featured image choice in create new/page|post
- Post Image not displaying in category view
- preg_replace not removed “class”
- Get attachments only for this post
- Upload attachment from external site
- Reinstall WordPress while keeping attachments
- how to give multiple post thumbnails to a post
- Display a mixed list of posts and media
- Using wp-cli and delete everypost first image
- How to display post title inside thumbnail?
- Post Title to featured Image ALT
- How to get all inline images in post content? [duplicate]
- Get featured image on hover of post title [closed]
- Get images from the post
- Remove Featured Image from posts in specific category?
- Inserting Media to WordPress Posts
- Retrieve a specific post’s featured image and show on a different page
- How To Download WordPress Audio Attachments
- How to get the next and previous image title from attachment or gallery?
- Only show featured image on exerpt and exclude images in post
- How to display Post title By ID and its Thumb Contents
- All images on one line for each post
- Delete Post but retain image of post [WP REST API]