24

I'm trying to download an image from a server using a PHP script on my website on xampp server.

The image is being downloaded using the function file_get_contents.

The php code for downloading on the server is:

if(isset($_GET['path']) && isset($_GET['username'])) {
    echo "path:".$_GET['path'];
    $temp = explode(".", $_GET['path']);
    $extension = end($temp);
    $fname="images/".$_GET['title'];
    $filenameIn  = $_GET['path'];
    $filenameOut = "" . $fname;
    $contentOrFalseOnFailure   = file_get_contents($filenameIn);
    $byteCountOrFalseOnFailure = file_put_contents($filenameOut,$contentOrFalseOnFailure);
}

But I'm getting this error:

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/xampp/project/upload_art.php on line 19

Warning: file_get_contents(http://app6.pixlr.com/_temp/528afa6e2f7cc6a5b1000101.jpg): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /opt/lampp/htdocs/xampp/project/upload_art.php on line 19

I have checked out all posted answers here but none seems to resolve the issue. Please help!

2

6 Answers 6

23

It means your server cannot connect to the outside world

This probably won't change anything given the DNS issues

So, If you have permission, try changing the name servers in your /etc/resolv.conf file to other nameservers.

2
  • like what? I hafe one IP only
    – samayo
    Commented Jun 9, 2015 at 22:38
  • 1
    I've added 8.8.8.8 and 8.8.4.4 and the issue is gone!
    – DUzun
    Commented Jan 3, 2017 at 11:37
12

There should be as well httpd allowed to connect outside. Check you selinux policy. this helps me to solve connection problem:

setsebool -P nis_enabled 1
setsebool -P httpd_can_network_connect 1
2
  • 1
    Same thing here, but neither of these options helped.
    – a coder
    Commented Jul 27, 2016 at 18:36
  • Also this one: setsebool -P httpd_verify_dns=1 and dont forgot to restart: ;) sudo systemctl restart httpd
    – Tharok
    Commented Oct 12, 2017 at 15:09
9

In reference to one of the comments by @DUzun, and for those like myself who are familiar with the server in general but not a computer science major, I fixed the issue on Linux/Centos6 (Plesk Onyx) by:

1) Opening /etc/resolv.conf (my file was empty)

2) Added:

nameserver 8.8.8.8
nameserver 8.8.4.4

options rotate
options timeout:3

Hopefully this helps someone out there.

1
  • I am facing this issue on Localhost on Windows 10 Machine. I am using Xampp. Any suggestion?
    – Kamlesh
    Commented Jul 11, 2021 at 15:31
2

Here are advice in case you are using Docker env for your web app. I had same problem when I used docker php image, added dns options in yml file solved my problem.

docker-compose.yml

version: '2'

services:
  webserver:
    build: ./docker/webserver
    image: my_webapp
    dns: 8.8.8.8
    dns_opt:
      - use-vc
      - no-tld-query
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./:/var/www/html
1

If you are using a name instead of an IP address in your smtp_host fsockopen() call, and your server cannot resolve that name into an IP address. If possible, find out the IP of the server you are trying to connect to, and use that instead. Hope so it will work fine. Example

$config = Array(
    'protocol' => 'smtp',
    'smtp_host' => '166.62.73.4', // mail.servername.com
    'smtp_port' => 587,
    'smtp_timeout' => '7',
    'smtp_user' =>'[email protected]',
    'smtp_pass' => 'password',
    '_smtp_auth'   => false,
    'mailtype'  => 'html', 
    'charset'   => 'iso-8859-1',
    'crlf' => '\r\n',
    'newline' => "\r\n"
            );
1

I had this error, when I have typo in url. Domain must exist:-)

1
  • 10 minutes searching on Google and then I found out that I wrote "localhsot" instead of "localhost"
    – Deazard
    Commented Dec 2, 2021 at 10:40

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