Downloading Files ( download_url() WP function) not working on local DevKinsta

Hello @rreyes Rex! :wave:

First of all, I’m not a programmer/developer, and I only tested the following PHP script to test the download_url() WP function (which I found on the internet and just modified it a bit) :smiley:

So, I created the following simple PHP script on my local DevKinsta site (named it as dlurl.php - located inside my ~/DevKinsta/public/agus-site/ folder):

<?php

include( 'wp-load.php' );

// If the function it's not available, require it.
if ( ! function_exists( 'download_url' ) ) {
	require_once ABSPATH . '/wp-admin/includes/file.php';
}

// Now you can use it!
$file_url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
$tmp_file = download_url( $file_url );

// Sets file final destination.
$filepath = ABSPATH . '/wp-content/uploads/downloaded-file-test.jpg';

// Copies the file to the final destination and deletes temporary file.
copy( $tmp_file, $filepath );
@unlink( $tmp_file );

The above code would download the image URL from the source:
https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg

to my local site folder as:
/wp-content/uploads/downloaded-file-test.jpg

Previously before I opened that PHP file (dlurl.php) locally on my browser, my ~/DevKinsta/public/agus-site/wp-content/uploads folder was showing this:

~/DevKinsta/public/agus-site/wp-content/uploads$ ll
total 12
drwxrwxrwx 3 agus agus 4096 Dec  8 08:15 ./
drwxrwxrwx 6 agus agus 4096 Dec  6 09:36 ../
drwxrwxrwx 8 agus agus 4096 Dec  5 06:50 2022/

And after I accessed that dlurl.php on my browser (e.g: https://agus-site.local/dlurl.php ), I could see the image file was downloaded properly into that “wp-content/uploads” sub-folder:

~/DevKinsta/public/agus-site/wp-content/uploads$ ll
total 40
drwxrwxrwx 3 agus agus  4096 Dec  8 08:22 ./
drwxrwxrwx 6 agus agus  4096 Dec  6 09:36 ../
drwxrwxrwx 8 agus agus  4096 Dec  5 06:50 2022/
-rw-r--r-- 1 agus agus 27661 Dec  8 08:22 downloaded-file-test.jpg

and I could also then access that downloaded .jpg file in question from my local site as expected.

You may want to give it a try with the sample PHP script/code above and see if it’s working fine as expected.
If it’s still not working, maybe you could please share the exact source URL to grab an image from?

Still unsure though, but just guessing for now, in case there’s a possibility if the external host/URL can’t be resolved from your local DevKinsta FPM container - similar to this issue before.
You may want to perform these as well to verify:

  1. run this command line: docker exec -it devkinsta_fpm bash (to connect to bash on the local FPM docker container)

  2. then inside that FPM container, please try to run something like thie:
    curl -I https://the-external-url.tld/of-the-image-file.jpg

  3. If it returns with something like “curl: (6) Could not resolve host: the-external-url.tld” , then most likely it’s an issue with your local DNS server resolver in that FPM container.
    And if that’s the case, you can run this command there:
    dns_server update -d the-external-url.tld

  4. Then see if you can run cURL command again of that external URL (as shown in step 2) above. If it resolves, then please try your PHP script/code again and see if it can download the image file in question from that external host URL.

Hopefully it will help you to solve the issue with the download_url() WP function (as that seems to be working fine as per my test on my local DevKinsta as mentioned above).

Cheers,
Agus