Question

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?

Était-ce utile?

La 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);

Autres conseils

file_get_contents() does not deal with slow network connections or redirects for getting remote files. You can use fsockopen() which allow a custom connection timeout value.

You will get more info from here.

You can also use curl to fetch remote files.You can see curl manual as well:

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top