问题
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:
- http://www.lloydsbaiahotel.it/images/bgtop/03.jpg
- http://www.lloydsbaiahotel.it/images/bgtop/04.jpg
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?
回答1:
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);
回答2:
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:
来源:https://stackoverflow.com/questions/18447827/file-get-contents-connection-timed-out-for-some-contents