So this seems like a pretty basic thing but I can't find a lot of documentation online about what's going on....
I'm trying to run through a list of files using Laravel 5.1 and I can only return/process/see the first file. I'm using Postman to send the request to the API (so I know multiple
is enabled in the POST
request) and then iterating through that a few different ways:
public function files(Request $request)
{
foreach($request->files as $file)
{
var_dump($file);
}
}
even:
public function files()
{
foreach($_FILES['files'] as $file)
{
var_dump($file);
}
}
I'm always returning (or the object form if the $request->files
method is used):
string 'Screen%20Shot%202015-10-23%20at%2010.07.23%20AM.png' (length=51)
string 'image/png' (length=9)
string '/tmp/phpZw1ALu' (length=14)
int 0
int 13687
Why is this happening? What can I do to see multiple files in Laravel 5.1's controllers?
So I made a brand new page with the following code called files.php
and it's now returning all three files (or however many I upload):
<!DOCTYPE>
<html>
<body>
<form method="post" enctype="multipart/form-data" action="http://lucolo.dev/files">
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
The error was that I was had the POST
request in Postman only accepting a parameter named files
instead of files[]
. Once that was changed, the code in my Laravel controller:
public function files(Request $request)
{
foreach($request->files as $file)
{
var_dump($file);
}
}
now returns:
array (size=3)
0 =>
object(Symfony\Component\HttpFoundation\File\UploadedFile)[84]
private 'test' => boolean false
private 'originalName' => string 'Screen Shot 2015-10-23 at 10.07.23 AM.png' (length=41)
private 'mimeType' => string 'image/png' (length=9)
private 'size' => int 270504
private 'error' => int 0
private 'pathName' (SplFileInfo) => string '/tmp/php1M5ZJl' (length=14)
private 'fileName' (SplFileInfo) => string 'php1M5ZJl' (length=9)
1 =>
object(Symfony\Component\HttpFoundation\File\UploadedFile)[85]
private 'test' => boolean false
private 'originalName' => string 'Screen Shot 2015-10-26 at 7.28.59 PM.png' (length=40)
private 'mimeType' => string 'image/png' (length=9)
private 'size' => int 13687
private 'error' => int 0
private 'pathName' (SplFileInfo) => string '/tmp/phpE22ubf' (length=14)
private 'fileName' (SplFileInfo) => string 'phpE22ubf' (length=9)
2 =>
object(Symfony\Component\HttpFoundation\File\UploadedFile)[86]
private 'test' => boolean false
private 'originalName' => string 'Screen Shot 2015-10-27 at 2.50.58 PM.png' (length=40)
private 'mimeType' => string 'image/png' (length=9)
private 'size' => int 786350
private 'error' => int 0
private 'pathName' (SplFileInfo) => string '/tmp/phph8v0C8' (length=14)
private 'fileName' (SplFileInfo) => string 'phph8v0C8' (length=9)
Hope that helps someone!