當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Server::updateFile方法代碼示例

本文整理匯總了PHP中Sabre\DAV\Server::updateFile方法的典型用法代碼示例。如果您正苦於以下問題:PHP Server::updateFile方法的具體用法?PHP Server::updateFile怎麽用?PHP Server::updateFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Sabre\DAV\Server的用法示例。


在下文中一共展示了Server::updateFile方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: httpPut

 /**
  * HTTP PUT method
  *
  * This HTTP method updates a file, or creates a new one.
  *
  * If a new resource was created, a 201 Created status code should be returned. If an existing resource is updated, it's a 204 No Content
  *
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @return bool
  */
 function httpPut(RequestInterface $request, ResponseInterface $response)
 {
     $body = $request->getBodyAsStream();
     $path = $request->getPath();
     // Intercepting Content-Range
     if ($request->getHeader('Content-Range')) {
         /**
            An origin server that allows PUT on a given target resource MUST send
            a 400 (Bad Request) response to a PUT request that contains a
            Content-Range header field.
         
            Reference: http://tools.ietf.org/html/rfc7231#section-4.3.4
         */
         throw new Exception\BadRequest('Content-Range on PUT requests are forbidden.');
     }
     // Intercepting the Finder problem
     if (($expected = $request->getHeader('X-Expected-Entity-Length')) && $expected > 0) {
         /**
         Many webservers will not cooperate well with Finder PUT requests,
         because it uses 'Chunked' transfer encoding for the request body.
         
         The symptom of this problem is that Finder sends files to the
         server, but they arrive as 0-length files in PHP.
         
         If we don't do anything, the user might think they are uploading
         files successfully, but they end up empty on the server. Instead,
         we throw back an error if we detect this.
         
         The reason Finder uses Chunked, is because it thinks the files
         might change as it's being uploaded, and therefore the
         Content-Length can vary.
         
         Instead it sends the X-Expected-Entity-Length header with the size
         of the file at the very start of the request. If this header is set,
         but we don't get a request body we will fail the request to
         protect the end-user.
         */
         // Only reading first byte
         $firstByte = fread($body, 1);
         if (strlen($firstByte) !== 1) {
             throw new Exception\Forbidden('This server is not compatible with OS/X finder. Consider using a different WebDAV client or webserver.');
         }
         // The body needs to stay intact, so we copy everything to a
         // temporary stream.
         $newBody = fopen('php://temp', 'r+');
         fwrite($newBody, $firstByte);
         stream_copy_to_stream($body, $newBody);
         rewind($newBody);
         $body = $newBody;
     }
     if ($this->server->tree->nodeExists($path)) {
         $node = $this->server->tree->getNodeForPath($path);
         // If the node is a collection, we'll deny it
         if (!$node instanceof IFile) {
             throw new Exception\Conflict('PUT is not allowed on non-files.');
         }
         if (!$this->server->updateFile($path, $body, $etag)) {
             return false;
         }
         $response->setHeader('Content-Length', '0');
         if ($etag) {
             $response->setHeader('ETag', $etag);
         }
         $response->setStatus(204);
     } else {
         $etag = null;
         // If we got here, the resource didn't exist yet.
         if (!$this->server->createFile($path, $body, $etag)) {
             // For one reason or another the file was not created.
             return false;
         }
         $response->setHeader('Content-Length', '0');
         if ($etag) {
             $response->setHeader('ETag', $etag);
         }
         $response->setStatus(201);
     }
     // Sending back false will interupt the event chain and tell the server
     // we've handled this method.
     return false;
 }
開發者ID:enoch85,項目名稱:owncloud-testserver,代碼行數:92,代碼來源:CorePlugin.php


注:本文中的Sabre\DAV\Server::updateFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。