diff --git a/src/Illuminate/Filesystem/FilesystemAdapter.php b/src/Illuminate/Filesystem/FilesystemAdapter.php index bd79e7feec55..e83c8127a5b2 100644 --- a/src/Illuminate/Filesystem/FilesystemAdapter.php +++ b/src/Illuminate/Filesystem/FilesystemAdapter.php @@ -241,14 +241,16 @@ public function putFile($path, $file, $options = []) * Store the uploaded file on the disk with a given name. * * @param string $path - * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile $file + * @param \Illuminate\Http\File|\Illuminate\Http\UploadedFile|string $file * @param string $name * @param array $options * @return string|false */ public function putFileAs($path, $file, $name, $options = []) { - $stream = fopen($file->getRealPath(), 'r'); + $filePath = is_string($file) ? $file : $file->getRealPath(); + + $stream = fopen($filePath, 'r'); // Next, we will format the path of the file and store the file using a stream since // they provide better performance than alternatives. Once we write the file this diff --git a/tests/Filesystem/FilesystemAdapterTest.php b/tests/Filesystem/FilesystemAdapterTest.php index 9e1df65ec37e..5638aa4f0624 100644 --- a/tests/Filesystem/FilesystemAdapterTest.php +++ b/tests/Filesystem/FilesystemAdapterTest.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Filesystem\FileExistsException; use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\FilesystemAdapter; +use Illuminate\Http\UploadedFile; use InvalidArgumentException; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; @@ -240,4 +241,34 @@ public function testPutWithStreamInterface() $spy->shouldHaveReceived('putStream'); $this->assertEquals('some-data', $filesystemAdapter->get('bar.txt')); } + + public function testPutFileAs() + { + file_put_contents($filePath = $this->tempDir.'/foo.txt', 'uploaded file content'); + + $filesystemAdapter = new FilesystemAdapter($this->filesystem); + + $uploadedFile = new UploadedFile($filePath, 'org.txt', null, null, true); + + $storagePath = $filesystemAdapter->putFileAs('/', $uploadedFile, 'new.txt'); + + $this->assertSame('new.txt', $storagePath); + + $this->assertFileExists($filePath); + + $filesystemAdapter->assertExists($storagePath); + + $this->assertSame('uploaded file content', $filesystemAdapter->read($storagePath)); + } + + public function testPutFileAsWithAbsoluteFilePath() + { + file_put_contents($filePath = $this->tempDir.'/foo.txt', 'normal file content'); + + $filesystemAdapter = new FilesystemAdapter($this->filesystem); + + $storagePath = $filesystemAdapter->putFileAs('/', $filePath, 'new.txt'); + + $this->assertSame('normal file content', $filesystemAdapter->read($storagePath)); + } }