当前位置: 首页>>代码示例>>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;未经允许,请勿转载。