public function testFile() { $key = 'formPutFile'; $token = $this->auth->uploadToken($this->bucketName, $key); list($ret, $error) = FormUploader::putFile($token, $key, __FILE__, $this->cfg, null, 'text/plain', true); $this->assertNull($error); $this->assertNotNull($ret['hash']); }
/** * 上传文件到七牛 * * @param $upToken 上传凭证 * @param $key 上传文件名 * @param $filePath 上传文件的路径 * @param $params 自定义变量,规格参考 * http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar * @param $mime 上传数据的mimeType * @param $checkCrc 是否校验crc32 * * @return array[] 包含已上传文件的信息,类似: * [ * "hash" => "<Hash string>", * "key" => "<Key string>" * ] */ public function putFile($upToken, $key, $filePath, $params = null, $mime = 'application/octet-stream', $checkCrc = false) { $file = fopen($filePath, 'rb'); if ($file === false) { throw new \Exception("file can not open", 1); } $params = self::trimParams($params); $stat = fstat($file); $size = $stat['size']; if ($size <= Config::BLOCK_SIZE) { $data = fread($file, $size); fclose($file); if ($data === false) { throw new \Exception("file can not read", 1); } return FormUploader::put($upToken, $key, $data, $params, $mime, $checkCrc); } $up = new ResumeUploader($upToken, $key, $file, $size, $params, $mime, $checkCrc); return $up->upload(); }
/** * Rewrite Qiniu\Storage\UploadManager::putFile * @param $upToken * @param $key * @param $fileResource * @param null $params * @param string $mime * @param bool $checkCrc * @return mixed * @throws \Exception */ private function qiniuPutFile($upToken, $key, $fileResource, $params = null, $mime = 'application/octet-stream', $checkCrc = false) { if ($fileResource === false) { throw new \Exception("file can not open", 1); } $file = $fileResource; $params = UploadManager::trimParams($params); $stat = fstat($file); $size = $stat['size']; if ($size <= QiniuConfig::BLOCK_SIZE) { $data = fread($file, $size); fclose($file); if ($data === false) { throw new \Exception("file can not read", 1); } $result = FormUploader::put($upToken, $key, $data, new QiniuConfig(), $params, $mime, $checkCrc); $this->lastQetag = $result['hash']; return $result; } $up = new ResumeUploader($upToken, $key, $file, $size, $params, $mime, new QiniuConfig()); $ret = $up->upload(); fclose($file); $this->lastQetag = $ret['hash']; return $ret; }