Last active
August 29, 2015 14:02
-
-
Save sergoslav/97724074e7c709951173 to your computer and use it in GitHub Desktop.
Multi upload images by CURL
This file contains 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 | |
/** | |
* Upload images | |
* | |
* @param $url | |
* @param $images | |
* @return array | |
*/ | |
function imageUpload($url, $images) | |
{ | |
$result = array(); | |
$handlers = array(); | |
$mh = curl_multi_init(); | |
$i = 0; | |
foreach($images as $image) | |
{ | |
$postParams = array('file' => "@".$image); | |
$handlers[$i] = curl_init(); | |
curl_setopt($handlers[$i], CURLOPT_URL, $url); | |
curl_setopt($handlers[$i], CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt( $handlers[$i], CURLOPT_POSTFIELDS, ;postParams); | |
curl_multi_add_handle($mh, $handlers[$i]); | |
$i++; | |
} | |
$running = null; | |
do | |
{ | |
curl_multi_exec($mh, $running); | |
} | |
while($running > 0); | |
foreach($handlers as $id => $val) | |
{ | |
if(!$val) | |
{ | |
$result[$id] = false; | |
} | |
else | |
{ | |
$result[$id] = curl_multi_getcontent($val); | |
curl_multi_remove_handle($mh, $val); | |
} | |
} | |
curl_multi_close($mh); | |
return $result; | |
} | |
$url = "asdasd.ru"; | |
$images = array( | |
'/var/tmp/file1.png', | |
'/var/tmp/file2.png', | |
'/var/tmp/file3.png' | |
); | |
$result = imageUpload($url, $images); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment