Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Allow absolute file path for Storage::putFileAs() #30918

Merged
merged 3 commits into from Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
}
}