Skip to content

Instantly share code, notes, and snippets.

@sergoslav
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergoslav/97724074e7c709951173 to your computer and use it in GitHub Desktop.
Save sergoslav/97724074e7c709951173 to your computer and use it in GitHub Desktop.
Multi upload images by CURL
<?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