From 73b19e69d6dbe681dca04f18f15a65f783074341 Mon Sep 17 00:00:00 2001 From: Jonathan Martins Date: Sun, 16 Nov 2014 21:03:30 -0200 Subject: [PATCH 1/4] File upload is broken when submitting a form without selecting a file. "I open a pull request here too, because I dont know the right place to put it. if is here in laravel framework, or there in the illuminate repository. https://github.com/illuminate/http/pull/14" File upload is broken when submitting a form without selecting a file in Laravel 5! My tests revealed that with this tweek the problem described at this issue are resolved: https://github.com/laravel/framework/issues/6189 Credits to @Marwelln This resolves a problem that occours in Symfony on the FileBag.php file, convertFileInformation() method. It receaves a empty array file and than returns NULL, if no files was selected on the upload. In this case "FileBag->set('image', NULL)" receives NULL, and that is what is dispatching a throw in Symfony. I misunderstood the problem, and try attempt to do a pull request on symfony, but it was denied: https://github.com/symfony/symfony/pull/12486 I don't make unit tests, but this will no more break my code flow. I'm not sure if this is the real deal! If someone could look deeper into the problem, I appreciated! --- src/Illuminate/Http/Request.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 21c09b9101bf..97262948ce2a 100755 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -599,11 +599,16 @@ public static function createFromBase(SymfonyRequest $request) { if ($request instanceof static) return $request; + $files = []; + foreach ($request->files->all() as $index => $file) { + if (null !== $file) $files[$index] = $file; + } + return (new static)->duplicate( - $request->query->all(), $request->request->all(), $request->attributes->all(), + $request->query->all(), $request->request->all(), $request->attributes->all(), - $request->cookies->all(), $request->files->all(), $request->server->all() + $request->cookies->all(), $files, $request->server->all() ); } From 61d05b001e6b0ba605f0736046144c08d736226e Mon Sep 17 00:00:00 2001 From: Jonathan Martins Date: Sun, 16 Nov 2014 21:50:50 -0200 Subject: [PATCH 2/4] [5.0] File upload is broken when submitting a form without selecting a file. Correct some tips by @GrahamCampbell --- src/Illuminate/Http/Request.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 97262948ce2a..7cdfd56f3fae 100755 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -601,14 +601,17 @@ public static function createFromBase(SymfonyRequest $request) $files = []; foreach ($request->files->all() as $index => $file) { - if (null !== $file) $files[$index] = $file; + if (null !== $file) + { + $files[$index] = $file; + } } return (new static)->duplicate( - $request->query->all(), $request->request->all(), $request->attributes->all(), + $request->query->all(), $request->request->all(), $request->attributes->all(), - $request->cookies->all(), $files, $request->server->all() + $request->cookies->all(), $files, $request->server->all() ); } From 7af7c4c5237d3f9c5961850fef2e9106c725f9c1 Mon Sep 17 00:00:00 2001 From: Jonathan Martins Date: Mon, 17 Nov 2014 18:57:38 -0200 Subject: [PATCH 3/4] No yoda conditions Thanks to @GrahamCampbell and @lucasmichot to teach me to not use Yoda conditions. --- src/Illuminate/Http/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 7cdfd56f3fae..7ce2291e6653 100755 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -601,7 +601,7 @@ public static function createFromBase(SymfonyRequest $request) $files = []; foreach ($request->files->all() as $index => $file) { - if (null !== $file) + if ($file !== null) { $files[$index] = $file; } From bd64792faef465da86ee66d489eab2b9830cc3fa Mon Sep 17 00:00:00 2001 From: Jonathan Martins Date: Mon, 17 Nov 2014 20:13:03 -0200 Subject: [PATCH 4/4] Using array filter to simplify the code. Credits for the though @pespantelis --- src/Illuminate/Http/Request.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 7ce2291e6653..8f6bea6481ee 100755 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -599,13 +599,10 @@ public static function createFromBase(SymfonyRequest $request) { if ($request instanceof static) return $request; - $files = []; - foreach ($request->files->all() as $index => $file) { - if ($file !== null) - { - $files[$index] = $file; - } - } + $files = array_filter($request->files->all(), function($file) + { + return $file !== null; + }); return (new static)->duplicate(