0

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?

  • if it does the job I'm not sure what the problem is, every snippet or tutorial had to be written once the first time, perhaps this is your opportunity! – Tom J Nowell Oct 19 '16 at 11:48
  • Yes it works. But, global $wp_filesystemdo same thing like 2nd way. I tried to use this global $wp_filesystem; in hooks init, admin_init But, This global variable does not works ( I don't know why ). Just want to know the valid way – maheshwaghmare Oct 19 '16 at 12:25
  • 1
    Ah after reading Marks answer I spotted that you're not copying files at all, you're doing a download of a remote file! I've updated your question accordingly, in which case neither of your solutions are how you should do that – Tom J Nowell Oct 19 '16 at 13:29
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.