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

[5.6] get contents of uploaded file #24924

Merged
merged 4 commits into from
Jul 27, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Illuminate/Http/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Arr;
use Illuminate\Container\Container;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;

Expand Down Expand Up @@ -86,6 +87,21 @@ public function storeAs($path, $name, $options = [])
);
}

/**
* Get the contents of the uploaded file.
*
* @return bool|string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getContents()
{
if ($this->isValid()) {
return file_get_contents($this->getPathname());
}

throw new FileNotFoundException("File does not exist at path {$this->getPathname()}");
}

/**
* Create a new file instance from a base instance.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Http/HttpUploadedFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Tests\Http;

use PHPUnit\Framework\TestCase;
use Illuminate\Http\UploadedFile;

class HttpUploadedFileTest extends TestCase
{
public function testUploadedFileCanRetrieveContentsFromTextFile()
{
$file = new UploadedFile(
__DIR__.'/fixtures/test.txt',
'test.txt',
null,
null,
null,
true
);

$this->assertEquals('This is a story about something that happened long ago when your grandfather was a child.', trim($file->getContents()));
}
}
1 change: 1 addition & 0 deletions tests/Http/fixtures/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a story about something that happened long ago when your grandfather was a child.