In this tutorial, we will learn how to securely validate file uploads in Laravel. Laravel provides various validation rules that can be applied to ensure that the files being uploaded meet specific criteria. This is important to ensure the security and integrity of your application.
By the end of this tutorial, you will learn:
Prerequisites: Basic knowledge of PHP and Laravel is required. Familiarity with Laravel's validation rules would be advantageous but not necessary.
The first step in validating file uploads is to create a form request. This is a special type of class in Laravel that encapsulates all the logic related to validating a request.
In Laravel, you can use the make:request
artisan command to generate a new form request class.
php artisan make:request UploadRequest
Laravel provides several validation rules that can be used for file uploads:
file
: The field under validation must be a file.image
: The file under validation must be an image (jpeg, png, bmp, gif, svg, or webp).mimes
: The file under validation must match one of the given MIME types.size
: The file under validation must have a size matching the given value.max
: The file under validation must not be larger than the given value.min
: The file under validation must have a minimum size given by the value.Here is an example of using these validation rules in a form request class:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UploadRequest extends FormRequest
{
public function rules()
{
return [
'file' => 'required|file|mimes:jpeg,png|max:2048',
];
}
}
In this example, the file
field is required, must be a file, must be either a jpeg or png, and must not exceed 2048 kilobytes.
To use this form request class, you would type-hint it in your controller method:
namespace App\Http\Controllers;
use App\Http\Requests\UploadRequest;
class UploadController extends Controller
{
public function store(UploadRequest $request)
{
// The incoming request is valid...
// Retrieve the validated input data...
$validated = $request->validated();
}
}
In this tutorial, we learned how to validate file uploads in Laravel securely. We covered various validation rules and how to create a form request class to encapsulate the validation logic.
For further learning, you can explore more complex validation scenarios and how to create custom validation rules.
Create a form that allows users to upload a PDF file. The file should not exceed 5MB.
Create a form that allows users to upload an image. The image must be either a jpeg or png and must not exceed 2MB.
Create a form that allows users to upload a CSV file. The file should not exceed 1MB.
public function rules()
{
return [
'file' => 'required|file|mimes:pdf|max:5120',
];
}
public function rules()
{
return [
'file' => 'required|image|mimes:jpeg,png|max:2048',
];
}
public function rules()
{
return [
'file' => 'required|file|mimes:csv,txt|max:1024',
];
}
Remember to replace 'file'
with the name of your form file input. Make sure to apply these rules in the appropriate form request, then use it in your controller.