Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Last active March 14, 2018 13:56
Show Gist options
  • Save jsdecena/ab6532b52470d52de1aade79ac9b7686 to your computer and use it in GitHub Desktop.
Save jsdecena/ab6532b52470d52de1aade79ac9b7686 to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Http\UploadedFile;
class SomeController extends Controller {
// loop example
public function store(Request $request)
{
if($request->hasFile('files')) {
collect($request->file('files'))->each(function(UploadedFile $file){
$this->validateFiles($file);
$filesname = $file->store('folder-name', ['disk' => 'public']);
// Will output: "folder-name/randomstring.jpg" (if you uploaded a jpeg)
dd($filename);
});
}
}
/**
*
* Validate the uploaded files
*/
private function validateFiles(UploadedFile $file)
{
// https://laravel.com/docs/5.6/validation#rule-file
$validator = Validator::make($request->all(), [
'file' => 'file,max:8000'
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment