Skip to content

Instantly share code, notes, and snippets.

@ManojKiranA
Last active November 5, 2024 11:18
Show Gist options
  • Save ManojKiranA/1b5004190610f7b99d2b440bfdcb5c28 to your computer and use it in GitHub Desktop.
Save ManojKiranA/1b5004190610f7b99d2b440bfdcb5c28 to your computer and use it in GitHub Desktop.
Sample File upload
Route::match(['get', 'post'], '/upload-file', function (\Illuminate\Http\Request $request) {
if ($request->isMethod('post')) {
$request->validate([
'file' => 'required|file|mimes:png,jpg,pdf,csv,txt|max:10240',
]);
dump($request->file('file'));
$path = $request
->file('file')
->store('uploads', 'public');
return back()->with('success', 'File uploaded successfully!')->with('path', $path);
}
return \Illuminate\Support\Facades\Blade::render('<html>
<body>
<h2>Upload a File</h2>
@if(session("success"))
<p style="color:green;">{{ session("success") }}</p>
<p>File Path: {{ session("path") }}</p>
@endif
<form action="' . route('upload-file') . '" method="POST" enctype="multipart/form-data">
' . csrf_field() . '
<label for="file">Choose file:</label>
<input type="file" name="file" id="file" required>
<br><br>
<button type="submit">Upload File</button>
</form>
</body>
</html>
');
})->name('upload-file');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment