/** * Reads out a file, and echos the content to the client. * * @param File $file File object * @return bool True is whole file is echoed successfully or false if client connection is lost in between */ protected function _sendFile($file) { $compress = $this->outputCompressed(); $file->open('rb'); $bufferSize = 8192; set_time_limit(0); session_write_close(); while (!feof($file->fileHandle())) { if (connection_status() !== CONNECTION_NORMAL or connection_aborted()) { $file->close(); return false; } echo fread($file->fileHandle(), $bufferSize); if (!$compress) { flush(); if (ob_get_level()) { ob_flush(); } } } $file->close(); return true; }
/** * testWrite method * * @return bool|void */ public function testWrite() { if (!($tmpFile = $this->_getTmpFile())) { return false; } if (file_exists($tmpFile)) { unlink($tmpFile); } $TmpFile = new File($tmpFile); $this->assertFalse(file_exists($tmpFile)); $this->assertFalse(is_resource($TmpFile->fileHandle())); $testData = ['CakePHP\'s', ' test suite', ' was here ...', '']; foreach ($testData as $data) { $r = $TmpFile->write($data); $this->assertTrue($r); $this->assertTrue(file_exists($tmpFile)); $this->assertEquals($data, file_get_contents($tmpFile)); $this->assertTrue(is_resource($TmpFile->fileHandle())); $TmpFile->close(); } unlink($tmpFile); }