Last active
March 14, 2018 13:56
-
-
Save jsdecena/ab6532b52470d52de1aade79ac9b7686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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