There is no need to use the WP filesystem API if you are trying to access directories to which the web server has full access. It is needed mainly only when you want to write to directories which have limited access like the plugin directory. From that point of view the two snippets are the same.
What you are potentially doing wrong is trying to access a remote resource with file API. Your first snippet will fail on some hosts and I am totally not sure if the WP filesystem API will work.
A more robust way is to use the wordpress HTTP to get the content of the file with wp_remote_get and save it to the uploads folders.
… last thing don’t do copyright infringements, too many people payed too much for such things.
Related Posts:
- Setting wp_temp_dir and permissions not working for “Missing A Temporary Folder” error
- Which filters or actions to use after a media upload and delete?
- Allowing all/different file type uploads
- Use WP_Filesystem to list files in directory
- wp_temp_dir does not change the /tmp temporary default directory
- Where is Featured Image code stored in WP?
- how to change max file upload size WordPress 4.9.8 [closed]
- Can’t write pdf file to upload directory using FPDF
- Change default uploads file using wp Skeleton
- Adding Adobe files to a wordpress site [closed]
- Placing assets for external use
- WordPress HTTP Error on File upload
- Interface for logged-in users to upload/download files
- WordPress Creates Unused (Unregistered) Image Sizes
- What functions of WP_Filesystem allow me to create a file with code-generated contents in a directory?
- Upload PDF and other files from the Front-End
- Uploaded images result in a file url with full path on disk appended
- temphangle variable missing when using wp_filesystem copy
- Where to store sensitive uploaded file?
- How to upload .tex files in wordpress?
- Downloadable content file structure
- Creating an .ics calendar subscription service with wordpress
- Should I use Filesystem API for reading files or listing directories?
- Private file system for attachments
- How to generate thumbnails when needed only?
- Media files exist in upload folder but not showing up
- Creating directory in uploads – wp_mkdir_p() or WP_Filesystem?
- Set limit to media upload?
- Save camera info as metadata on image upload?
- Modify featured image path to Amazon S3
- How to set an upload directory for each media type?
- How to protect uploads in multisite if user is not logged in?
- Trigger JS when featured image upload window is opened in admin
- Modify the array of selected images in media modal
- How can I upload SVG images using the media uploader?
- Delete images uploaded by ‘Subscriber’ role
- post_id missing from the wp-admin file upload request
- Insert Featured image from Feed
- Image uploading stuck on “crunching”
- HTTP Error when uploading mp4 video file
- Change the size of the image preview on the media edit page
- “upload_mimes” filter not working. Need to allow WOFF and WOFF2
- Saving WordPress generated thumbnails in a subdirectory
- Add select field to media uploader that adds a class to the image
- Retroactively place uploaded media into -month, -year based folders?
- Can’t seem to upload a file to a custom directory
- wp.media add context
- Conditional add_filter for upload directory?
- Need to download uploaded binary file
- How do I modify the url of uploaded media content?
- Can I delete `wp-config-sample.php` after installing and configuring WordPress?
- WordPress uploads folder path. how it is decided?
- Uploading images on front end doesn’t generate thumbnail sizes
- Filter / Hook to get attachment ID before uploading?
- Adding Media Upload to Custom Options Panel breaks “Insert into Post”
- PDF file upload issue
- WordPress bug with capabilities?
- Update image via WP REST API
- Media Library does not show the uploaded image
- Attach Thumbnail Generated from Video Upload as Featured Image for the Video
- ‘An error occurred in the upload. Please try again later.’ for users with different roles
- Manipulate CSV data during upload, is it possible?
- Check if uploaded file has a specific filename
- How to submit data between wp_iframe and backbone.js in media upload
- Uploading .mp3 files http error – 2 “exact” files one successful other fails
- HTTP Error after finishing uploading a video that is bigger than 64 MB -> After raising the upload limit manually in the wp-config file
- Set post_parent value for the images uploaded by Add Media button into post
- File Upload from Frontend
- Easing the download of a regularly uploaded pdf
- Get all images in uploads directory and list them
- Upload mime types for ai illustrator file
- Is it possible to import all files from a uploads DIR into WP media, retaining paths to the files
- How to resize attachement
- Offload Media Library to other domain on a different server
- Programmatically Upload File to WordPress Using an API [closed]
- Add SWF file to wordpress through custom template
- Uploading flash flipbook to mu wordpress site
- Change Link to Upload folder
- download images from wp-content/uploads/year/month/DSC_123.jpg
- Which wordpress action fires after crunching
- Workaround to upload AVIF files
- Images not displaying on site or media library
- Changing WordPress media location and upload path to sub-domain not working
- Hide obj path in source code
- Media not displaying other users uploads – WordPress 4.9.2
- Unable to upload anything to WordPress site
- Uploads from site on Server A uploaded on Server B (Like sort of CDN)
- Creating an Uploads folder with post ID
- “Could not write file” error in wp_upload_bits function
- Add media button does not insert image in the editor
- How to find out if all enqueued files are uploaded?
- Limit WP Uploader to certain file types only in pages
- controlling whether upload is attached to post or not
- Attachment Metadata not updated while uploading audio files
- Is it possible to upload files for wordpress importer over FTP?
- Basic File/Post restriction plugin
- http upload error as a subscribed user
- Limit max upload file size in front end submmission form
- How do I restore attachment from files in wp-upload
- Different upload file size limits based on file type