+2 votes
in Programming Languages by (4.6k points)

I am trying to fetch images file from YouTube using file_get_contents(), but it takes 10-20 seconds to fetch one image file of size 15-25kb.

$cont=file_get_contents($img_url);

How can I speed up the download of images?

1 Answer

0 votes
by (21.4k points)

There may be some issues with your server settings, otherwise, file_get_contents() should not be that slow. I would recommend using curl instead of file_get_contents(). Your download speed will improve with curl.

Try the following PHP code that uses curl to fetch the content from a URL.

$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $img_url);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
$cont = curl_exec($ci);
curl_close($ci);

...