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

I am programmatically creating posts and use the Wordpress download_url() function to grab an image and put it in the media library. I spent a couple of hours trying to figure out why it wasnt working and then decided to push my work to the Kinsta staging site. Well there it worked perfectly.

What is different with DevKinsta in that it cant download files from URLs?

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

Thanks for the detailed repsonse. I still cannot get it working tho. I tried ssh’ing to the container. While the curl command did work with the wikimedia image, it is not working with my image ( https://mesilinka-video.s3-us-west-2.amazonaws.com/MF001_E001-1920-00001.jpg ) . The error i get is curl: (6) Could not resolve host: mesilinka-video.s3-us-west-2.amazonaws.com

Like i said this all works fine on my staging server at Kinsta so for now i am just working there. I would like for my local enviroment to work the same tho.

Hey @rreyes , just to clarify, does the issue persist even after following the steps listed here or by following the step number 3 shared by Agus?

Regards,

Alessandro

Hello Rex @rreyes ,

I couldn’t replicate this inside the docker FPM container on my Linux machine, but was able to replicate that cURL issue inside the docker FPM container on my Windows machine (which seems to be the DNS resolver issue in there).

This what I got when I tested on my Windows machine:

C:\Users\iagus>docker exec -it devkinsta_fpm bash
root@b2953a82ac4c:/www/kinsta# curl -I https://mesilinka-video.s3-us-west-2.amazonaws.com/MF001_E001-1920-00001.jpg
curl: (6) Could not resolve host: mesilinka-video.s3-us-west-2.amazonaws.com

I then tried to add that that external hostname with the following (as I mentioned in my previous reply - step 3 above):

dns_server update -d mesilinka-video.s3-us-west-2.amazonaws.com

but still that didn’t resolve that external hostname via cURL:

root@b2953a82ac4c:/www/kinsta# dns_server update -d mesilinka-video.s3-us-west-2.amazonaws.com
{"domain":"mesilinka-video.s3-us-west-2.amazonaws.com.","records":[{"ttl":60,"class":"IN","type":"A","address":""}]}
root@b2953a82ac4c:/www/kinsta# curl -I https://mesilinka-video.s3-us-west-2.amazonaws.com/MF001_E001-1920-00001.jpg
curl: (6) Could not resolve host: mesilinka-video.s3-us-west-2.amazonaws.com

Then I checked further on my Linux machine (same network/ISP as my Windows machine), and I could see the hostname: mesilinka-video.s3-us-west-2.amazonaws.com resolves to multiple A records/IP addresses, and the results might change/vary from different locations (and from your location as well):

$ host mesilinka-video.s3-us-west-2.amazonaws.com
mesilinka-video.s3-us-west-2.amazonaws.com is an alias for s3-r-w.us-west-2.amazonaws.com.
s3-r-w.us-west-2.amazonaws.com has address 3.5.80.14
s3-r-w.us-west-2.amazonaws.com has address 3.5.81.171
s3-r-w.us-west-2.amazonaws.com has address 52.218.237.185
s3-r-w.us-west-2.amazonaws.com has address 52.218.237.225
s3-r-w.us-west-2.amazonaws.com has address 52.92.192.114
s3-r-w.us-west-2.amazonaws.com has address 52.92.147.34
s3-r-w.us-west-2.amazonaws.com has address 52.218.241.65
s3-r-w.us-west-2.amazonaws.com has address 52.218.221.49

You can also check the same with this 3rd party tool:

So I altered the command a bit to be something like this on my Windows / local docker FPM container (I added the first 3 IPs showed on my Linux terminal above):

dns_server add -d mesilinka-video.s3-us-west-2.amazonaws.com -A the.ip.address.listed.above

root@b2953a82ac4c:/www/kinsta# dns_server add -d mesilinka-video.s3-us-west-2.amazonaws.com -A 3.5.80.14
{"domain":"mesilinka-video.s3-us-west-2.amazonaws.com.","records":[{"ttl":60,"class":"IN","type":"A","address":"3.5.80.14"},{"ttl":60,"class":"IN","type":"A","address":""}]}
root@b2953a82ac4c:/www/kinsta# dns_server add -d mesilinka-video.s3-us-west-2.amazonaws.com -A 3.5.81.171
{"domain":"mesilinka-video.s3-us-west-2.amazonaws.com.","records":[{"ttl":60,"class":"IN","type":"A","address":"3.5.81.171"},{"ttl":60,"class":"IN","type":"A","address":"3.5.80.14"},{"ttl":60,"class":"IN","type":"A","address":""}]}
root@b2953a82ac4c:/www/kinsta# dns_server add -d mesilinka-video.s3-us-west-2.amazonaws.com -A 52.218.237.185
{"domain":"mesilinka-video.s3-us-west-2.amazonaws.com.","records":[{"ttl":60,"class":"IN","type":"A","address":"52.218.237.185"},{"ttl":60,"class":"IN","type":"A","address":"3.5.81.171"},{"ttl":60,"class":"IN","type":"A","address":"3.5.80.14"},{"ttl":60,"class":"IN","type":"A","address":""}]}

After that, I was able to perform the cURL command line to access that image URL (external host) inside the docker FPM container on my Windows machine:

root@b2953a82ac4c:/www/kinsta# curl -I https://mesilinka-video.s3-us-west-2.amazonaws.com/MF001_E001-1920-00001.jpg
HTTP/1.1 200 OK
x-amz-id-2: 4romAENZjlpN+NUibPf1pLY4NnD79axdqmxLzUPHNJRc287PS0HQ5Ni8CTaRVMyVmmhcUUSOSfI=
x-amz-request-id: ZR4CCCC3XES0NFAG
Date: Fri, 09 Dec 2022 03:49:17 GMT
Last-Modified: Thu, 27 Oct 2022 16:41:03 GMT
ETag: "a0760718792e9085e132d07a57760296"
Accept-Ranges: bytes
Content-Type: image/jpeg
Server: AmazonS3
Content-Length: 51453

I suspect you might be using Windows as well? if that so, please try the steps I mentioned above and see if it will help to resolve that external host name on your local docker FPM container . Once the cURL can resolve that URL/hostname, then you should be able to access the same URL from your PHP scripts.

Cheers,
Agus