示例#1
0
 public function testChecksEof()
 {
     $handle = fopen('php://temp', 'w+');
     fwrite($handle, 'data');
     $stream = new Stream($handle);
     $this->assertFalse($stream->eof());
     $stream->read(4);
     $this->assertTrue($stream->eof());
     $stream->close();
 }
 /**
  * Return the contents of the file
  *
  * @return string
  */
 public function getContents()
 {
     // If an offset is provided
     if ($this->offset !== -1) {
         // Seek to the offset
         $this->stream->seek($this->offset);
     }
     // If a max length is provided
     if ($this->maxLength !== -1) {
         // Read from the offset till the maxLength
         return $this->stream->read($this->maxLength);
     }
     return $this->stream->getContents();
 }
示例#3
0
 /**
  * Download a given File.
  *
  * @param mixed $file File Location or Resource
  *
  * @return resource
  */
 protected function downloadFile($file)
 {
     if (!is_resource($file)) {
         $file = fopen($file, 'r');
     }
     $stream = new Stream($file);
     $downloadedFile = fopen('php://temp', 'w+');
     if ($downloadedFile === false) {
         throw new \Exception('Error when saving the downloaded file');
     }
     while (!$stream->eof()) {
         $writeResult = fwrite($downloadedFile, $stream->read(8000));
         if ($writeResult === false) {
             throw new \Exception('Error when saving the downloaded file');
         }
     }
     $stream->close();
     rewind($downloadedFile);
     return $downloadedFile;
 }