Skip to content

Commit

Permalink
[11.x] Fix client path value in file uploads (#53941)
Browse files Browse the repository at this point in the history
* [11.x] Fix client path value in file uploads

* update composer.json | http-foundation

* fix style
  • Loading branch information
gyaaniguy authored Dec 17, 2024
1 parent b6f5f02 commit 4526005
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"symfony/console": "^7.0.3",
"symfony/error-handler": "^7.0.3",
"symfony/finder": "^7.0.3",
"symfony/http-foundation": "^7.0.3",
"symfony/http-foundation": "^7.2.0",
"symfony/http-kernel": "^7.0.3",
"symfony/mailer": "^7.0.3",
"symfony/mime": "^7.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Http/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static function createFromBase(SymfonyUploadedFile $file, $test = false)
{
return $file instanceof static ? $file : new static(
$file->getPathname(),
$file->getClientOriginalName(),
$file->getClientOriginalPath(),
$file->getClientMimeType(),
$file->getError(),
$test
Expand Down
46 changes: 46 additions & 0 deletions tests/Http/HttpUploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;

class HttpUploadedFileTest extends TestCase
{
Expand All @@ -19,4 +20,49 @@ public function testUploadedFileCanRetrieveContentsFromTextFile()

$this->assertSame('This is a story about something that happened long ago when your grandfather was a child.', trim($file->get()));
}

public function testUploadedFileInRequestContainsOriginalPathAndName()
{
$symfonyFile = new SymfonyUploadedFile(__FILE__, '');
$this->assertSame('', $symfonyFile->getClientOriginalName());
$this->assertSame('', $symfonyFile->getClientOriginalPath());
$file = UploadedFile::createFromBase($symfonyFile);
$this->assertSame('', $file->getClientOriginalName());
$this->assertSame('', $file->getClientOriginalPath());

$symfonyFile = new SymfonyUploadedFile(__FILE__, 'test.txt');
$this->assertSame('test.txt', $symfonyFile->getClientOriginalName());
$this->assertSame('test.txt', $symfonyFile->getClientOriginalPath());
$file = UploadedFile::createFromBase($symfonyFile);
$this->assertSame('test.txt', $file->getClientOriginalName());
$this->assertSame('test.txt', $file->getClientOriginalPath());

$symfonyFile = new SymfonyUploadedFile(__FILE__, '/test.txt');
$this->assertSame('test.txt', $symfonyFile->getClientOriginalName());
$this->assertSame('/test.txt', $symfonyFile->getClientOriginalPath());
$file = UploadedFile::createFromBase($symfonyFile);
$this->assertSame('test.txt', $file->getClientOriginalName());
$this->assertSame('/test.txt', $file->getClientOriginalPath());

$symfonyFile = new SymfonyUploadedFile(__FILE__, '/foo/bar/test.txt');
$this->assertSame('test.txt', $symfonyFile->getClientOriginalName());
$this->assertSame('/foo/bar/test.txt', $symfonyFile->getClientOriginalPath());
$file = UploadedFile::createFromBase($symfonyFile);
$this->assertSame('test.txt', $file->getClientOriginalName());
$this->assertSame('/foo/bar/test.txt', $file->getClientOriginalPath());

$symfonyFile = new SymfonyUploadedFile(__FILE__, '/foo/bar/test.txt');
$this->assertSame('test.txt', $symfonyFile->getClientOriginalName());
$this->assertSame('/foo/bar/test.txt', $symfonyFile->getClientOriginalPath());
$file = UploadedFile::createFromBase($symfonyFile);
$this->assertSame('test.txt', $file->getClientOriginalName());
$this->assertSame('/foo/bar/test.txt', $file->getClientOriginalPath());

$symfonyFile = new SymfonyUploadedFile(__FILE__, 'file:\\foo\\test.txt');
$this->assertSame('test.txt', $symfonyFile->getClientOriginalName());
$this->assertSame('file:/foo/test.txt', $symfonyFile->getClientOriginalPath());
$file = UploadedFile::createFromBase($symfonyFile);
$this->assertSame('test.txt', $file->getClientOriginalName());
$this->assertSame('file:/foo/test.txt', $file->getClientOriginalPath());
}
}

0 comments on commit 4526005

Please sign in to comment.