# Downloading Files ( download\_url() WP function) not working on local DevKinsta

**URL:** https://community.kinsta.com/t/downloading-files-download-url-wp-function-not-working-on-local-devkinsta/1935
**Category:** Application Support
**Created:** [December 7, 2022, 11:52pm UTC](https://community.kinsta.com/t/downloading-files-download-url-wp-function-not-working-on-local-devkinsta/1935 "2022-12-07T23:52:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rreyes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.kinsta.com/rreyes/32/1451_2.png) [@rreyes](https://community.kinsta.com/u/rreyes)
#### Post date: [December 7, 2022, 11:52pm UTC](https://community.kinsta.com/t/downloading-files-download-url-wp-function-not-working-on-local-devkinsta/1935/1 "2022-12-07T23:52:53Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Agus](https://sea2.discourse-cdn.com/flex020/user_avatar/community.kinsta.com/agus/32/1515_2.png) [@Agus](https://community.kinsta.com/u/Agus)
#### Post date: [December 8, 2022, 1:45am UTC](https://community.kinsta.com/t/downloading-files-download-url-wp-function-not-working-on-local-devkinsta/1935/2 "2022-12-08T01:45:13Z")

</div>

Hello @rreyes Rex! 👋

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) 😃

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):

```auto
<?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:

```auto
~/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:

```auto
~/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.

 ![Screenshot from 2022-12-08 08-24-16](https://us1.discourse-cdn.com/flex020/uploads/kinsta/original/2X/1/167344e8604a34adbd702cb7a67b6b154fc37dc1.png)

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](https://community.kinsta.com/t/curl-cant-resolve-host-img-youtube-com/1895/5) 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

---

<div class="post-metadata">

### Author: ![rreyes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.kinsta.com/rreyes/32/1451_2.png) [@rreyes](https://community.kinsta.com/u/rreyes)
#### Post date: [December 8, 2022, 2:54pm UTC](https://community.kinsta.com/t/downloading-files-download-url-wp-function-not-working-on-local-devkinsta/1935/3 "2022-12-08T14:54:01Z")

</div>

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](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.

---

<div class="post-metadata">

### Author: ![Alessandro](https://sea2.discourse-cdn.com/flex020/user_avatar/community.kinsta.com/alessandro/32/2310_2.png) [@Alessandro](https://community.kinsta.com/u/Alessandro)
#### Post date: [December 8, 2022, 3:19pm UTC](https://community.kinsta.com/t/downloading-files-download-url-wp-function-not-working-on-local-devkinsta/1935/4 "2022-12-08T15:19:07Z")

</div>

Hey @rreyes , just to clarify, does the issue persist even after following the steps listed [here](https://community.kinsta.com/t/curl-cant-resolve-host-img-youtube-com/1895/5) or by following the step number 3 shared by Agus?

Regards,

Alessandro

---

<div class="post-metadata">

### Author: ![Agus](https://sea2.discourse-cdn.com/flex020/user_avatar/community.kinsta.com/agus/32/1515_2.png) [@Agus](https://community.kinsta.com/u/Agus)
#### Post date: [December 9, 2022, 3:54am UTC](https://community.kinsta.com/t/downloading-files-download-url-wp-function-not-working-on-local-devkinsta/1935/5 "2022-12-09T03:54:35Z")

</div>

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:

```auto
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](http://mesilinka-video.s3-us-west-2.amazonaws.com)

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

```auto
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):

```auto
$ 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:

> **[DNS Propagation Checker - Global DNS Checker Tool](https://www.whatsmydns.net/#A/mesilinka-video.s3-us-west-2.amazonaws.com)**
>
> Instant DNS Propagation Check. Global DNS Propagation Checker - Check DNS records around the world.

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](http://mesilinka-video.s3-us-west-2.amazonaws.com) -A the.ip.address.listed.above

```auto
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:

```auto
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
