php - Upload multiple files in Laravel 4 -
here controller code uploading multiple files , passing key , value 'postman' rest api client on google chrome. adding multiple files postman 1 file getting upload.
public function post_files() { $allowedexts = array("gif", "jpeg", "jpg", "png","txt","pdf","doc","rtf","docx","xls","xlsx"); foreach($_files['file'] $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." 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 redirect message file uploaded // return redirect::to('authors') } else { return response::json('error', 400); } } } } }
i have tried code returns me location of file in temporary folder
$file = input::file('file'); echo count($file);
and echo count($_files['file']);
returns me 5.can tell me why?
and why foreach(input::file('file') $key => $abc)
gives error invalid arguments
not using api, might outline principle.
i set routes.php file, whick upload test.
routes.php
// save files route::post('upload', function(){ $files = input::file('files'); foreach($files $file) { // public/uploads $file->move('uploads/'); } }); // show form route::get('/', function() { echo form::open(array('url' => 'upload', 'files'=>true)); echo form::file('files[]', array('multiple'=>true)); echo form::submit(); echo form::close(); });
notice input name, files[]: if uploading multiple files under same name, include brackets well.
Comments
Post a Comment