Right way to download file from source to destination

I'm trying to download a file from one location to another. The code is working perfect. Here is the code snippet:

Helper Function - Create directory in /uploads/{$dir_name}/ and return paths.

/**
 * Create folder in /uploads/{$dir_name}/
 * @return array()
 */
function get_dirpath( $dir_name = 'test' ) {
    
    $wp_info  = wp_upload_dir();

    // SSL workaround.
    if ( is_ssl() ) {
        $wp_info['baseurl'] = str_ireplace( 'http://', 'https://', $wp_info['baseurl'] );
    }

    // Build the paths.
    $dir_info = array(
        'path'   = $wp_info['basedir'] . '/' . $dir_name . '/',
        'url'    = $wp_info['baseurl'] . '/' . $dir_name . '/'
    );

    // Create the upload dir if it doesn't exist.
    if ( ! file_exists( $dir_info['path'] ) ) {

        // Create the directory.
        mkdir( $dir_info['path'] );

        // Add an index file for security.
        file_put_contents( $dir_info['path'] . 'index.html', '' );
    }

    return $dir_info;
}

Way - 1 - Using PHP copy() function - {works perfect}.

$dir_info    = get_dirpath( 'test' );
$remote_file = 'https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
$local_file  = trailingslashit( $dir_info['path'] ) . basename( $remote_file );

if ( copy( $remote_file, $local_file ) ) {
    //  Successfully copy file from URL
}

Way - 2 - Using WordPress WP_Filesystem_Direct - {works perfect}

require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php';
require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php';
$my_filesystem = new WP_Filesystem_Direct( array() );

if ( $my_filesystem-copy( $remote_file, $local_file, true) ) {
    //  Successfully copy file from URL
}

Both, Works perfect.

This code snippet:

  • Create the file googlelogo_color_272x92dp.png
  • In directory /wp-content/uploads/test/
  • From source https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

I think, I need to use Way - 2. But, Is this right way to include files and create new WP_Filesystem_Direct() object?

I search little bit regarding and found Copy a file from a plugin into my theme directory and How to use copy() function and paste file in /wp-content/themes directory

But, Not found any solution / snippet which use the Way - 2.

Edited: - The global $wp_filesystem does same thing from way - 2. But, Its not working ( Don't know why! ).


Updated

Also, How to get valid error message if it fail the file download?

Topic filesystem-api wp-filesystem filesystem uploads Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.