Reputation: 131
I need to upload multiple files in laravel 4 I have written the following code in controller
public function post_files()
{
$allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf");
$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>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " 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
}
else
{
return Response::json('error', 400);
}
}
}
}
else
{
return "Invalid file";
}
}
}
I am able to upload a single file via this code but not able to upload multiple files. Please help me in this isue..Thank you in advance..
public function post_abc()
{
// $file = Input::file('file'); // your file upload input field in the form should be named 'file'
$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 = 'abc/'.$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 )
{
return "hello";
}
else
{
return Response::json('error', 400);
}
}
}
}
}
Using postman rest client on google chrome I have passed key as 'file' and value as '3 files selected' and input type is file
Upvotes: 0
Views: 4948
Reputation: 131
Issue solved. I changed the code as follows in the controller:
public function post_multiple()
{
// $file = Input::file('file'); // your file upload input field in the form should be named 'file'
$allowedExts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf","docx","xls","xlsx");
if(isset($_FILES['image']['tmp_name']))
{
$i=0;
foreach(Input::file('image') as $key => $file)
{
$temp = explode(".", $_FILES["image"]["name"][$i]);
$extension = end($temp);
$filename= $temp[0];
$destinationPath = 'abc/'.$filename.'.'.$extension;
if(in_array($extension, $allowedExts)&&($_FILES["image"]["size"][$i] < 20000000))
{
if($_FILES["image"]["error"][$i] > 0)
{
echo "Return Code: " . $_FILES["image"]["error"][$i] . "<br>";
}
if (file_exists($destinationPath))
{
echo $filename." already exists. ";
}
else
{
$uploadSuccess=move_uploaded_file($_FILES["image"]["tmp_name"][$i],$destinationPath);
if( $uploadSuccess )
{
$name=$filename.".".$extension;
$document_details=Response::json(Author::insert_document_details_Call($name,$destinationPath));
echo "Update";
}
else
{
return Response::json('error', 400);
}
}
}
$i++;
}
}
}
And in View part I had written name=image
instead of image[]
Upvotes: 1
Reputation: 795
Call This part into a loop
if(in_array($extension, $allowedExts)&&($_FILES["file"]["size"] < 20000000))
{
if($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " 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
}
else
{
return Response::json('error', 400);
}
}
}
}
else
{
return "Invalid file";
}
Upvotes: 1