diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index c17d83931bd328c697c7a5bf403d2ab2add87233..e8300cfc60fa40edc0f1388210c22501528694f9 100644 --- a/Entity/UploadedFile.php +++ b/Entity/UploadedFile.php @@ -384,20 +384,24 @@ class UploadedFile * @param int $writeOffset * @return int */ - public function copyFrom($source, $maxlen = PHP_INT_MAX, $writeOffset = 0) + public function copyFrom($source, $maxlen = -1, $writeOffset = 0) { if($maxlen === 0) { return 0; } - $stream = $this->filesystem->createStream($this->getPath(), new StreamMode('cb')); + $stream = $this->filesystem->createStream($this->getPath()); + $stream->open(new StreamMode('cb')); $stream->seek($writeOffset); - if(false !== $fileHandler = $stream->cast(STREAM_CAST_AS_STREAM)) { + if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) { // Utilise stream_copy_to_stream si le Gaufrette\Stream peut nous retourner un filehandle - $copied = $this->stream_copy_to_stream($source, $fileHandler, $maxlen); + $copied = $this->stream_copy_to_stream($source, $fileHandle, $maxlen); } else { // Sinon fait une copie par blocs (moins performant) + if($maxlen === -1) { + $maxlen = PHP_INT_MAX; + } $copied = 0; while(!$this->feof($source) && $copied <= $maxlen) { $copied += $stream->write($this->fread($source, min(static::$copyBlockSize, $maxlen - $copied))); @@ -417,13 +421,18 @@ class UploadedFile */ public function copyTo($dest, $maxlen = PHP_INT_MAX, $readOffset = 0) { - $actualLength = min($maxlen, $this->getSize() - $readOffset); + if($maxlen === -1) { + $actualLength = $this->getSize() - $readOffset; + } else { + $actualLength = min($maxlen, $this->getSize() - $readOffset); + } if (0 <= $actualLength) { return 0; } - $stream = $this->filesystem->createStream($this->getPath(), new StreamMode('rb')); + $stream = $this->filesystem->createStream($this->getPath()); + $stream->open(new StreamMode('rb')); $stream->seek($readOffset); if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) {