See media_handle_sideload
in Codex:
$url = "http://s.wordpress.org/style/images/wp3-logo.png";
$tmp = download_url( $url );
$post_id = 1;
$desc = "The WordPress Logo";
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error( $tmp ) ) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload( $file_array, $post_id, $desc );
// If error storing permanently, unlink
if ( is_wp_error($id) ) {
@unlink($file_array['tmp_name']);
return $id;
}
$src = wp_get_attachment_url( $id );
Related Posts:
- How does WP media uploader create the 3 different sized images, and how can I duplicate it
- How to assign multiple file-mime-types to extension?
- Is it possible to reorganize the WordPress uploads directory?
- Create image formats with different qualities when uploading
- Which filters or actions to use after a media upload and delete?
- wp_delete_attachment doesn’t delete images in wp-content/uploads/
- Force WordPress 3.3 to use Flash uploader
- How to restrict images in v3.5 Media Library modal to only those from a specific post id?
- Allow CSV files to be uploaded
- How to add more upload directories?
- Multisite, upload images directly to Amazon S3
- How can I speed up a slow loading media library?
- What to do with unattached logos and header uploaded via native wordpress uploader?
- upload_mimes filter has no effect
- How can you limit the number of images / videos that can be uploaded to a WordPress post
- Failure upgrading / updating site to WordPress 4.7
- Impose a Maximum Limit on Image height and width upload size
- Force wordpress to see uploads folder media
- Moving Media Library
- Add item to media library from blob or dataUrl
- How do I enable the customize theme page to accept svg’s?
- Disable TinyMCE Drag and Drop
- WP3.5 Media Uploader – how to make it accept multiple images?
- How to define a remote uploads directory?
- Allowing all/different file type uploads
- resize images not crop
- Trying to add filename over image in Media Browser
- Upload image from a URL and assign it as a Featured Image to a Custom Post Type?
- Setting wp_temp_dir and permissions not working for “Missing A Temporary Folder” error
- How to hook into wordpress admin upload iframe?
- frontend upload return async-ajax.php 302
- Prevent File Uploads other than images
- Media library storing files in uploads not folders within in uploads
- upload files dynamically to user folders
- How to protect media directory with .htaccess?
- Is there a limit on the number of attachments?
- ACF attachment custom field in rest response
- Why does SVG upload in Media Library fail if the file does not have an XML tag at the beginning?
- media_handle_upload : undefined function?
- How can I allow upload of ttf or otf font files when hooking `upload_mimes` doesn’t work
- Can I stop wordpress generating media sizes
- I made a website using WordPress, but the server is ColdFusion, how can I make it work?
- Unable to create directory uploads/2018/12. Is its parent directory writable by the server?
- Calling WP Gallery Uploader/Selector From Metabox
- why am i getting the ‘fatal error’ msg when uploading photos within specified MB?
- Stop WordPress from generating redundant image size
- WordPress 4.9.8 Trying to add filename over image in Media Browser
- Disable image thumbnails for only one upload folder
- Media library only shows for admin. Doesn’t show editor or below [closed]
- Upload featured image from front end using media-upload.php
- Get File Object from wp.Uploader
- media_handle_sideload on a file already on server
- Capability to prevent upload of files
- HTTP Error When Uploading Images with HTTPS?
- Saving file to disk receiving fopen error
- Check for file extension or mime type fails
- A link (not in the post) to download a specific PDF file
- Set different max upload size limits based on file type/extension
- Correctly using the root directory for media uploads?
- how to change wp upload directory to another domain directory
- Populate a custom attachment metadata field with data from the image’s EXIF data?
- Disable “Create Audio Playlist” and “Create Video Playlist” in Add Media
- Change file name on upload in Media Page
- What is the URL pattern for image thumbnails + sizes?
- Setting up a HIPAA secured form / file upload
- How to make picture appear on wordpress site shared link? [closed]
- How to download files to WordPress?
- How to detect when a file has been uploaded?
- wp-content/uploads returns a 404
- Cant upload larger than 100MB
- PDF Upload from Input Error
- Multiple images are generated in WP-content in wordpress
- Removing extra large generated images disables all crops
- MIME types not recognized by media library
- Missing a temporary folder not fixed
- How to allow .xls file uploads?
- Upload automatically html files
- Updating the attachment from front end doesn’t show the new change
- Is there some way to find if media items are used anywhere on the site?
- wp_update_attachment_metadata breaks after 10 files
- Media URL slugs without uploads folder path
- Add more field on WordPress media upload
- Frontend media shows “An error occurred in the upload. Please try again later.”
- File Upload Management
- WP_Custom_Media_Control | Give ID to media file
- WP Add Media Button – Can I set the default size to Original/Full Size
- Video limit about wordpress
- Can i add flickr,imgur etc. up-loader for my WP upload media button?
- Files larger than 500 kilobytes are not allowed
- How to split my uploaded media into directories?
- get the post thumbnail returns the full size
- All files unattached in Media Library
- Media Upload Directory to MMYY instead of YYYY/MM
- Auto-Import of WXR File
- how upload images and videos to specific folder like wp-content\uploads\folder-name
- Media upload takes too long
- Problems with defining UPLOADS constant
- Updating Media Library PDF’s in bulk
- Assign multiple categories to Media Library upload
- Update media item using wordpress rest api in python