Skip to content

Commit

Permalink
[6.x] Allow absolute file path for Storage::putFileAs() (#30918)
Browse files Browse the repository at this point in the history
* Update FilesystemAdapter.php

* allow absolute file path for Storage::putFileAs

* update test
  • Loading branch information
SjorsO authored and taylorotwell committed Dec 25, 2019
1 parent 1b2779c commit bc21598
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/Filesystem/FilesystemAdapterTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit bc21598

Please sign in to comment.