I know this is a little necro, but other people may want to find out how it works.
Here, as a normal twisted file addition unit would look like:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ret['Location']); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_PUT, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_READFUNCTION, 'curlPutThrottle'); curl_setopt($ch, CURLOPT_INFILE, $fh); curl_setopt($ch, CURLOPT_INFILESIZE, $size); $ret = curl_exec($ch);
and the read function will look something like this (this throttle is dependent on the user speed $ goal and gives feedback to the CLI display)
function curlPutThrottle($ch, $fh, $length = false) { global $size; global $current; global $throttle; global $start; $goal = (300*1024*1024)/(60*10); if (!$length) { $length = 1024 * 1024; } if (!is_resource($fh)) { return 0; } $current += $length; if ($current > $throttle) { $pct = round($current/$size*100); $disp = "Uploading (".$pct."%) - ".number_format($current, 0).'/'.number_format($size, 0); echo "\r ".$disp.str_repeat(" ", strlen($disp)); $throttle += 1024*1024; $elapsed = time() - $start; $expectedUpload = $goal * $elapsed; if ($current > $expectedUpload) { $sleep = ($current - $expectedUpload) / $goal; $sleep = round($sleep); for ($i = 1; $i <= $sleep; $i++) { echo "\r Throttling for ".($sleep - $i + 1)." Seconds - ".$disp; sleep(1); } echo "\r ".$disp.str_repeat(" ", strlen($disp)); } } if ($current > $size) { echo "\n"; } return fread($fh, $length); }
Where:
- $ ch - cURL resource that calls the ReadFunction function
- $ fh is the file descriptor from CURLOPT_INFILE
- $ length is the amount of data that it expects to receive.
It returns data from a file of length $ length or `` if EOF.
source share