Machen Sie eine FormRequest
Objekt, indem Sie den folgenden Befehl ausgeben:
php artisan make:request YourFormRequest
Nun, in Ihrer Regelmethode:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'filename' => 'mimes:pdf,doc,jpeg,png,docx',
// and other validation rules...
];
}
Aktualisieren Sie jetzt Ihren Controller:
/**
* Store the form values.
* Don't forget to import the YourFormRequest class
*
* @param \App\Http\Requests\YourFormRequest $request
* @return \Illuminate\Http\Redirect|string
*/
public function store(YourFormRequest $request)
{
if($request->file('filename')) {
$file = $request->file('filename');
$fileName = $file->getClientOriginalName();
$fileExt = $file->getClientOriginalExtension();
$fileMime = $file->getClientMimeType();
// and rest of the file details
// Move the file now
$updatedFileName = $fileName.'.'.$fileExt;
$file->move('path/to/destination/folder', $updatedFileName);
// or using the Storage class, it is the same
// as what you have written.
}
}
AKTUALISIERUNG 1:
In Ihrem YourFormRequest
Datei, ersetzen Sie die Autorisierungsmethode:
/**
* Authorize the request.
*
* @return bool
*/
public function authorize()
{
return true; // replace false with true.
}
Hoffe das hilft dir weiter. Prost.