Skip to content

Commit a4550da

Browse files
committed
feature #37479 [HttpFoundation] Added File::getContent() (lyrixx)
This PR was merged into the 5.2-dev branch. Discussion ---------- [HttpFoundation] Added File::getContent() | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | --- This is a very simple PR but I think it increases the DX when working with uploaded files: I looked at for this method and I did not found it. So I had to check witch method returns the "pathname" or to remember I can cast the object to string to get its pathname. It's a small detail, but IMHO it's better with it. About file_get_contents vs stream: let's be simple here. If one want a stream, they can use the code they always used for it :) Commits ------- 50d5167 [HttpFoundation] Added File::getContent()
2 parents 502d6fb + 50d5167 commit a4550da

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added `HeaderUtils::parseQuery()`: it does the same as `parse_str()` but preserves dots in variable names
8+
* added `File::getContent()`
89

910
5.1.0
1011
-----

src/Symfony/Component/HttpFoundation/File/File.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ public function move(string $directory, string $name = null)
104104
return $target;
105105
}
106106

107+
public function getContent(): string
108+
{
109+
$content = file_get_contents($this->getPathname());
110+
111+
if (false === $content) {
112+
throw new FileException(sprintf('Could not get the content of the file "%s".', $this->getPathname()));
113+
}
114+
115+
return $content;
116+
}
117+
107118
/**
108119
* @return self
109120
*/

src/Symfony/Component/HttpFoundation/Tests/File/FileTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public function testMoveWithNewName()
8585
@unlink($targetPath);
8686
}
8787

88+
public function testGetContent()
89+
{
90+
$file = new File(__FILE__);
91+
92+
$this->assertStringEqualsFile(__FILE__, $file->getContent());
93+
}
94+
8895
public function getFilenameFixtures()
8996
{
9097
return [

0 commit comments

Comments
 (0)