/** * Write using a stream * * @param $path * @param $resource * @param null $config * @return array|bool */ public function writeStream($path, $resource, Config $config) { $username = '******'; $password = '******'; $auth = base64_encode($username . ':' . $password); $this->client->setUri('http://192.168.10.47/api/wstest/upload/fg'); // $this->client->setUri('http://files.local/api/v2/fs'); $this->client->setMethod('POST'); $headers = ['Accept' => '*/*', 'Cache-Control' => 'no-cache', 'Authorization' => 'Basic ' . $auth, 'X-File-Name' => 'todo.txt', 'Content-Type' => 'application/x-www-form-urlencoded']; $this->client->setHeaders($headers); file_put_contents('tmp', $resource); // $this->client->setFileUpload('todo.txt', 'todo_txt', $resource); $text = 'this is some plain text'; $this->client->setFileUpload('some_text.txt', 'some_text_txt', $text, 'text/plain'); prn($this->client->send()->getContent()); exit; $location = $this->applyPathPrefix($path); $config = Util::ensureConfig($config); $this->ensureDirectory(dirname($location)); $this->client->setMethod('POST'); $this->client->setUri($this->api_url); $this->client->setParameterPOST(array_merge($this->auth_param, ['filename' => $path])); $this->client->setHeaders(['wp_path' => $path]); file_put_contents('tmp', $resource); $this->client->setFileUpload('tmp', 'form'); $response = $this->client->send(); $path = json_decode($response->getContent())->file_id; $type = 'wepo_fs'; $result = compact('contents', 'type', 'size', 'path'); if ($visibility = $config->get('visibility')) { $result['visibility'] = $visibility; } return $result; }
protected function _replace($filePath, $photoId, $async = 0) { $params['async'] = $async; $params['photo_id'] = $photoId; $finalParams = $this->_httpUtility->assembleParams($this->_endpointReplace, $this->_configOAuth, $params); $request = new \Zend\Http\Request(); $request->setUri($this->_endpointReplace)->setMethod('POST')->setPost(new Parameters($finalParams)); $this->_httpClient->reset(); $this->_httpClient->setRequest($request); $this->_httpClient->setEncType(\Zend\Http\Client::ENC_FORMDATA, 'ITSCARO'); $this->_httpClient->setFileUpload($filePath, 'photo'); $response = $this->_httpClient->dispatch($request); $decodedResponse = simplexml_load_string($response->getBody()); if (!$decodedResponse instanceof \SimpleXMLElement) { throw new \Exception('Could not decode response: ' . $response->getBody(), self::ERR_RESPONSE_NOT_XML); } else { if ($decodedResponse['stat'] == 'ok') { if ($async) { return (string) $decodedResponse->ticketid; } else { return (string) $decodedResponse->photoid; } } else { throw new \Exception((string) $decodedResponse->err['msg'], (int) $decodedResponse->err['code']); } } }
/** * @param string|array $filePaths * * @return Model\File[] * @throws \Zend_Http_Client_Exception */ private function _uploadExistingFile($filePaths) { $files = (array) $filePaths; foreach ($files as $file) { $this->_client->setFileUpload($file, 'files[]'); } return $this->_upload(); }
/** * Sends a request. * * @param string $url The url. * @param string $method The http method. * @param array $headers The headers. * @param array $content The content. * @param array $files The files. * * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. * * @return \Widop\HttpAdapter\HttpResponse The response. */ private function sendRequest($url, $method, array $headers = array(), array $content = array(), array $files = array()) { $this->client->resetParameters()->setOptions(array('maxredirects' => $this->getMaxRedirects()))->setMethod($method)->setUri($url)->setHeaders($headers)->setParameterPost($content); foreach ($files as $key => $file) { $this->client->setFileUpload($file, $key); } try { $response = $this->client->send(); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); } return $this->createResponse($response->getStatusCode(), $url, $response->getHeaders()->toArray(), $method === Request::METHOD_HEAD ? '' : $response->getBody()); }
/** * Test that one can upload multiple files with the same form name, as an * array * * @link http://framework.zend.com/issues/browse/ZF-5744 */ public function testMutipleFilesWithSameFormNameZF5744() { if (!ini_get('file_uploads')) { $this->markTestSkipped('File uploads disabled.'); } $rawData = 'Some test raw data here...'; $this->client->setUri($this->baseuri . 'testUploads.php'); $files = array('file1.txt', 'file2.txt', 'someotherfile.foo'); $expectedBody = ''; foreach ($files as $filename) { $this->client->setFileUpload($filename, 'uploadfile[]', $rawData, 'text/plain'); $expectedBody .= "uploadfile {$filename} text/plain " . strlen($rawData) . "\n"; } $this->client->setMethod('POST'); $res = $this->client->send(); $this->assertEquals($expectedBody, $res->getBody(), 'Response body does not include expected upload parameters'); }
/** * * @param type $assetType * @param type $post * @return boolean */ public function uploadAsset($assetType, $post) { $filename = strtolower(str_replace("_", "", $post['assetType'])) . '-' . $post['uniqueId']; $filepath = realpath($post['Filedata']['tmp_name']); $finename = $post['Filedata']['name']; $extension = pathinfo($finename, PATHINFO_EXTENSION); $destination = $assetType->getPath() . '/' . $filename . "." . $extension; $client = new Client(); $client->setMethod('POST'); $client->setUri('http://imageserver.local.com/Upload.php'); $client->setFileUpload($filepath, 'filepath'); $client->setParameterPost(array('destination' => $destination)); $response = $client->send(); if ($response->isSuccess()) { return array('filepath' => $response->getContent(), 'filename' => $filename); } }
/** * Prepare a POST request for uploading files * * The request method will be set to POST and multipart/form-data will be used * as content type, ignoring the current request format. * * $files is treated as: * [ * name => localFilePath, * ... * ] * * $data will be used for other data data without the filename segment. * * Each localFilePath will be read and sent. Will try to guess the content type using mime_content_type(). * By default, the basename of localFilePath will be sent as filename segment, if a 'name' is also present * in $data then the value of $data[name] will be used as filename segment. * * * * @param array $files * @param string $relativePath * @param array $data * @param array $query * @return \Zend\Http\Request */ public function prepareFileUploadRequest(array $files, $relativePath = null, array $data = [], array $query = []) { $request = $this->prepareRequest('POST', $relativePath, [], $query); $request->getHeaders()->removeHeader($request->getHeaders()->get('Content-Type')); $this->httpClient->setRequest($request); foreach ($files as $formName => $filePath) { $this->httpClient->setFileUpload($filePath, $formName); if (isset($data[$formName])) { $file = $request->getFiles()->get($filePath, null); if ($file) { // If present, override the filename $file['filename'] = $data[$formName]; $request->getFiles()->set($filePath, $file); } unset($data[$formName]); } } $request->getPost()->fromArray($data); return $request; }
/** * {@inheritdoc} */ public function write($path, $contents, Config $config) { $location = $this->applyPathPrefix($path); prn($path, $contents, $config, $location, dirname($location)); $this->client->setMethod('POST'); $this->client->setUri($this->api_url); $this->client->setParameterPOST(array_merge($this->auth_param, ['filename' => $path])); file_put_contents('tmp', $contents); $this->client->setFileUpload('tmp', 'form'); $response = $this->client->send(); if ($response->getStatusCode() != 200) { throw new \Exception(json_decode($response->getBody())->message); } prn($response->getContent()); return json_decode($response->getContent())->data->{$filename}; $location = $this->applyPathPrefix($path); $this->client->request('PUT', $location, $contents); $result = compact('path', 'contents'); if ($config->get('visibility')) { throw new LogicException(__CLASS__ . ' does not support visibility settings.'); } return $result; }
/** * Goes recursively through the files array and adds uploads to the ZendClient */ protected function addFileUploadsRecursively(ZendClient $client, array $files, $arrayName = '') { foreach ($files as $name => $info) { if (!empty($arrayName)) { $name = $arrayName . '[' . $name . ']'; } if (isset($info['tmp_name']) && isset($info['name'])) { if ('' !== $info['tmp_name'] && '' !== $info['name']) { $filename = $info['name']; if (false === ($data = @file_get_contents($info['tmp_name']))) { throw new \RuntimeException("Unable to read file '{$filename}' for upload"); } $client->setFileUpload($filename, $name, $data); } } elseif (is_array($info)) { $this->addFileUploadsRecursively($client, $info, $name); } } }
/** * Prepare the client to send the file and params in request * * @param \Zend\Http\Client $client * @param Request $request * @return void */ protected function prepareFileUpload($client, $request) { $filename = $request->getFileUpload(); $client->setFileUpload('content', 'content', file_get_contents($filename), 'application/octet-stream; charset=binary'); }