Performance: File uploads
File uploads impact page load speed and interaction responsiveness by affecting server response time and client-side resource usage during upload.
<?php // Controller method using chunked upload or queue public function upload(Request $request) { $file = $request->file('upload'); dispatch(new ProcessUploadJob($file)); return response()->json(['message' => 'Upload started'], 202); }
<?php
// Controller method handling upload synchronously
public function upload(Request $request) {
$file = $request->file('upload');
$file->move(public_path('uploads'), $file->getClientOriginalName());
return back()->with('success', 'File uploaded');
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous upload handling | Minimal | 0 | 0 | [✗] Bad |
| Asynchronous upload with queue | Minimal | 0 | 0 | [✓] Good |
hasFile to verify if a file was uploaded in the request.hasFile is the correct method; others do not exist in Laravel.enctype="multipart/form-data" to send file data properly.method="post" is needed but not enough alone; type="file" is for input, not form; action defines URL, not upload encoding.photo.jpg?
$path = $request->file('photo')->store('images');
return $path;store() method saves the file in the specified folder with a unique generated filename to avoid overwriting.if ($request->hasFile('avatar')) {
$file = $request->file('avatar');
$file->move('uploads');
}move() method requires two arguments: destination path and filename.photos?foreach ($request->file('photos') as $photo) {
$path = $photo->store('gallery');
Photo::create(['path' => $path]);
} loops over each file, stores it, and saves path correctly. Others treat files as single or misuse methods.Pass the test and this pattern moves unknown