/** * 上传操作 */ public function upload() { $uploaded = 0; while ($uploaded < $this->size) { $blockSize = $this->blockSize($uploaded); $data = fread($this->inputStream, $blockSize); if ($data === false) { fclose($this->inputStream); throw new \Exception("file read failed", 1); } $crc = \Jw\Upload\QiNiu\Support\crc32_data($data); $response = $this->makeBlock($data, $blockSize); $ret = null; if ($response->ok() && $response->json() != null) { $ret = $response->json(); } if ($response->statusCode < 0) { $this->host = $this->config->getUpHostBackup(); } if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) { $response = $this->makeBlock($data, $blockSize); $ret = $response->json(); } if (!$response->ok() || !isset($ret['crc32']) || $crc != $ret['crc32']) { fclose($this->inputStream); return array(null, new Error($this->currentUrl, $response)); } array_push($this->contexts, $ret['ctx']); $uploaded += $blockSize; } fclose($this->inputStream); return $this->makeFile(); }
public static function put($upToken, $key, $data, $config, $params, $mime, $checkCrc) { $fields = array('token' => $upToken); if ($key === null) { $fname = 'filename'; } else { $fname = $key; $fields['key'] = $key; } if ($checkCrc) { $fields['crc32'] = \Jw\Upload\QiNiu\Support\crc32_data($data); } if ($params) { foreach ($params as $k => $v) { $fields[$k] = $v; } } $response = Client::multipartPost($config->getUpHost(), $fields, 'file', $fname, $data, $mime); if (!$response->ok()) { return array(null, new Error($config->getUpHost(), $response)); } return array($response->json(), null); }