Cake\Network\Response::file PHP Метод

file() публичный Метод

If $_SERVER['HTTP_RANGE'] is set a slice of the file will be returned instead of the entire file. ### Options keys - name: Alternate download name - download: If true sets download header and forces file to be downloaded rather than displayed in browser
public file ( string $path, array $options = [] ) : void
$path string Path to file. If the path is not an absolute path that resolves to a file, `APP` will be prepended to the path.
$options array Options See above.
Результат void
    public function file($path, array $options = [])
    {
        $options += ['name' => null, 'download' => null];
        if (strpos($path, '../') !== false || strpos($path, '..\\') !== false) {
            throw new NotFoundException('The requested file contains `..` and will not be read.');
        }
        if (!is_file($path)) {
            $path = APP . $path;
        }
        $file = new File($path);
        if (!$file->exists() || !$file->readable()) {
            if (Configure::read('debug')) {
                throw new NotFoundException(sprintf('The requested file %s was not found or not readable', $path));
            }
            throw new NotFoundException(__d('cake', 'The requested file was not found'));
        }
        $extension = strtolower($file->ext());
        $download = $options['download'];
        if ((!$extension || $this->type($extension) === false) && $download === null) {
            $download = true;
        }
        $fileSize = $file->size();
        if ($download) {
            $agent = env('HTTP_USER_AGENT');
            if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
                $contentType = 'application/octet-stream';
            } elseif (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
                $contentType = 'application/force-download';
            }
            if (!empty($contentType)) {
                $this->type($contentType);
            }
            if ($options['name'] === null) {
                $name = $file->name;
            } else {
                $name = $options['name'];
            }
            $this->download($name);
            $this->header('Content-Transfer-Encoding', 'binary');
        }
        $this->header('Accept-Ranges', 'bytes');
        $httpRange = env('HTTP_RANGE');
        if (isset($httpRange)) {
            $this->_fileRange($file, $httpRange);
        } else {
            $this->header('Content-Length', $fileSize);
        }
        $this->_file = $file;
    }

Usage Example

Пример #1
0
 /**
  * Convert a PSR7 Response into a CakePHP one.
  *
  * @param \Psr\Http\Message\ResponseInterface $response The response to convert.
  * @return \Cake\Network\Response The equivalent CakePHP response
  */
 public static function toCake(PsrResponse $response)
 {
     $body = static::getBody($response);
     $data = ['status' => $response->getStatusCode(), 'body' => $body['body']];
     $cake = new CakeResponse($data);
     if ($body['file']) {
         $cake->file($body['file']);
     }
     $cookies = static::parseCookies($response->getHeader('Set-Cookie'));
     foreach ($cookies as $cookie) {
         $cake->cookie($cookie);
     }
     $headers = static::collapseHeaders($response);
     $cake->header($headers);
     if (!empty($headers['Content-Type'])) {
         $cake->type($headers['Content-Type']);
     }
     return $cake;
 }
All Usage Examples Of Cake\Network\Response::file