Search code examples
phpfile-get-contentsconnection-timeout

file_get_contents Connection Timed Out for some contents


i'm developing a site in which user can upload pictures.

Once users insert the url ( only remote ) of image, i call a ajax to a php that take url and store it to the server.

The problem is the file_get_contents:

For some images it gives me Connection Timed Out, for other no.

Some images that gives me that error are:

I simple try to fetch content in this way:

$img   = "http://www.lloydsbaiahotel.it/images/bgtop/03.jpg";
$inbuf = file_get_contents($img);
var_dump($inbuf);

What could be the problem? Do i have to change some configurations of my server?


Solution

  • try to use cURL , it gives you more options and control than file_get_contents as the following :

      $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $raw=curl_exec($ch);
        curl_close ($ch);
        $fp = fopen($save_to,'x');
        fwrite($fp, $raw);
        fclose($fp);