Here is my controller code for uploading multiple files and I am passing key and value from 'postman' rest API client on Google Chrome. I am adding multiple files from postman but only 1 file is getting upload.
public function post_files() {
$allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf","docx","xls","xlsx");
foreach($_FILES['file'] as $key => $abc) {
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$filename= $temp[0];
$destinationPath = 'upload/'.$filename.'.'.$extension;
if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000)) {
if($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
if (file_exists($destinationPath)) {
echo $filename." already exists. ";
} else {
$uploadSuccess=move_uploaded_file($_FILES["file"]["tmp_name"],$destinationPath);
if( $uploadSuccess ) {
$document_details=Response::json(Author::insert_document_details_Call($filename,$destinationPath));
return $document_details; // or do a redirect with some message that file was uploaded
// return Redirect::to('authors')
} else {
return Response::json('error', 400);
}
}
}
}
}
I have tried this code also but it returns me location of a file in temporary folder
$file = Input::file('file');
echo count($file);
and
echo count($_FILES['file']);
returns me always 5.Can anyone tell me why?
and why foreach(Input::file('file') as $key => $abc)
gives the error invalid arguments
the above solutions wont work with multiple files as the return will fire as soon the first item gets validated ,so here is a solution after several hours of head-wall banging. inspired by https://www.youtube.com/watch?v=PNtuds0l8bA
u basicly save the filenames in an array and pass those arrays to a session then only add
return
when the loop has finished.Not using any API, but this might outline the principle.
I set up this routes.php file, whick will help you with upload test.
routes.php
Notice the input name, files[]: If uploading multiple files under the same name, include brackets as well.
1. Form :- Form opening tag must have ‘files’=>true and file field must have name [](with array) and ‘multiple’=>true
2. Add below code to your controller function :-
SOURCE : http://tutsnare.com/upload-multiple-files-in-laravel/
EDIT: Reference source link is not working.
Solution:
You can get all files by simply doing:
Explanation:
The class
Input
is actually a Facade for theIlluminate\Http\Request
class (Yes, just like theRequest
facade - they both serve as the "Face" for the same class!**).That means you can use any methods available in Request .
If we search for the function
file()
, we see it works like this:Now,
retrieveItem()
is a protected method, so we can't just call it from our Controller directly. Looking deeper, however, we see that we can pass thefile()
method "null" for the key. If we do so, then we'll get all the items!So, if we call
Input::file()
, the Request class will internally run$this->retrieveItem('files', null, null)
which will in turn runreturn $this->files->all();
and we will get all the files uploaded.** Note that
Input
Facade has the extra method get() available in it.