file() public method

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.
return void
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;
 }
Example #2
0
 /**
  * test getFile method
  *
  * @return void
  */
 public function testGetFile()
 {
     $response = new Response();
     $this->assertNull($response->getFile(), 'No file to get');
     $response->file(TEST_APP . 'vendor/css/test_asset.css');
     $file = $response->getFile();
     $this->assertInstanceOf('Cake\\Filesystem\\File', $file, 'Should get a file');
     $this->assertPathEquals(TEST_APP . 'vendor' . DS . 'css' . DS . 'test_asset.css', $file->path);
 }
Example #3
0
 /**
  * test file with .. in a path fragment
  *
  * @expectedException \Cake\Network\Exception\NotFoundException
  * @expectedExceptionMessage my/ca..t/image.gif was not found or not readable
  * @return void
  */
 public function testFileWithDotsInAPathFragment()
 {
     $response = new Response();
     $response->file('my/ca..t/image.gif');
 }
 /**
  * test file with ..
  *
  * @expectedException \Cake\Network\Exception\NotFoundException
  * @expectedExceptionMessage my/ca..t.gif was not found or not readable
  * @return void
  */
 public function testFileWithDotIntheName()
 {
     $response = new Response();
     $response->file('my/ca..t.gif');
 }
Example #5
0
 /**
  * test file with ..
  *
  * @expectedException \Cake\Network\Exception\NotFoundException
  * @return void
  */
 public function testFileWithPathTraversal()
 {
     $response = new Response();
     $response->file('my/../cat.gif');
 }
Example #6
0
 /**
  * Sends an asset file to the client
  *
  * @param \Cake\Network\Request $request The request object to use.
  * @param \Cake\Network\Response $response The response object to use.
  * @param string $assetFile Path to the asset file in the file system
  * @param string $ext The extension of the file to determine its mime type
  * @return \Cake\Network\Response The updated response.
  */
 protected function _deliverAsset(Request $request, Response $response, $assetFile, $ext)
 {
     $compressionEnabled = $response->compress();
     if ($response->type($ext) === $ext) {
         $contentType = 'application/octet-stream';
         $agent = $request->env('HTTP_USER_AGENT');
         if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
             $contentType = 'application/octetstream';
         }
         $response->type($contentType);
     }
     if (!$compressionEnabled) {
         $response->header('Content-Length', filesize($assetFile));
     }
     $response->cache(filemtime($assetFile), $this->_cacheTime);
     $response->file($assetFile);
     return $response;
 }