Created
August 13, 2019 22:26
-
-
Save victormattosvm/c12e8512385b5d501916d9475ba9d026 to your computer and use it in GitHub Desktop.
Fastest way to download an array of images using multicurl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Fastest way to download an array of images using multicurl | |
*/ | |
function multi_download(array $urls, $pathToSave) | |
{ | |
$multi_handle = curl_multi_init(); | |
$file_pointers = []; | |
$curl_handles = []; | |
if(!file_exists($pathToSave)){ | |
//create folder | |
mkdir( $pathToSave, 0775 ); | |
} | |
foreach ($urls as $key => $url) { | |
$filename = basename($url); | |
$file = $pathToSave . '/' . $filename; | |
$curl_handles[$key] = curl_init($url); | |
if(!file_exists($file)) { | |
$file_pointers[$key] = fopen($file, "w"); | |
curl_setopt($curl_handles[$key], CURLOPT_AUTOREFERER, TRUE); | |
curl_setopt($curl_handles[$key], CURLOPT_HEADER, 0); | |
curl_setopt($curl_handles[$key], CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl_handles[$key], CURLOPT_TIMEOUT, 30); //30s | |
curl_setopt($curl_handles[$key], CURLOPT_FOLLOWLOCATION, TRUE); | |
curl_setopt($curl_handles[$key], CURLOPT_SSL_VERIFYHOST, FALSE); | |
curl_setopt($curl_handles[$key], CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($curl_handles[$key], CURLOPT_FILE, $file_pointers[$key]); | |
curl_multi_add_handle($multi_handle,$curl_handles[$key]); | |
} | |
} | |
// Download the files | |
do { | |
curl_multi_exec($multi_handle,$running); | |
} while ($running > 0); | |
// Free up objects | |
foreach ($urls as $key => $url) { | |
curl_multi_remove_handle($multi_handle, $curl_handles[$key]); | |
curl_close($curl_handles[$key]); | |
if($file_pointers[$key]){ | |
fclose ($file_pointers[$key]); | |
} | |
} | |
curl_multi_close($multi_handle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment