Laravel - Video file validation

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Laravel Video File Validation: A Comprehensive Approach Body:

Video file validation is an essential security measure to ensure that only intended media files are uploaded by users onto your web application. In this blog post, we will discuss how to validate video files using Laravel and provide a more comprehensive approach to tackle the issue raised in the original question.

The traditional way: The initial code snippet demonstrated a common validation method used in Laravel to validate file uploads. However, it has some drawbacks as mentioned in the comments. Here's how to improve it:

$validator = Validator::make(Input::all(),
  array(
    'file'  => 'mimes:mp4,mov,ogg | max:20000'
  )
);

This code snippet checks if the uploaded file has one of the specified mime types (mp4, mov, or ogg) and also limits its size to be less than 20000 bytes. It doesn't check for other video formats like FLV, MP4, MPEGURL, etc., which may cause confusion and incorrect file validation in some situations.

A better approach: To address the issues mentioned above, a more practical approach is to utilize Laravel's built-in features. Firstly, store the uploaded file's mime type as a variable:

$file = Input::file('file');
$mime = $file->getMimeType();

Then perform an if statement to check for video-specific mime types. Here's the code:

if ($mime == "video/x-flv" || $mime == "video/mp4" || $mime == "application/x-mpegURL" || $mime == "video/MP2T" || $mime == "video/3gpp" || $mime == "video/quicktime" || $mime == "video/x-msvideo" || $mime == "video/x-ms-wmv") 
{
  // process upload
}
By using this approach, you ensure that the Laravel validation rule only checks for video mimetypes and not other file types. This reduces confusion and helps maintain a more accurate validation system.

Alternative method: If your application requires validation of multiple types of files (not just videos) or the above approach seems too complex, you can use Laravel's built-in storage rules. You could create a function that accepts an uploaded file and determines its mime type:

function determineMimeType($file) {
  $mime = $file->getClientMimeType();

  switch ($mime) {
    case "video/x-flv":
      return 'video/FLV';

    // add more cases for other video mimetypes here...

    default:
      return false;
  }
}

Then, make sure you validate the uploaded file's mime type using this function in your validation rules:

$validator = Validator::make(Input::all(),
  array(
    'file' => "required|mimes:".determineMimeType($file)."|max:20000"
  )
);
In conclusion, the original answer was not wrong, but it failed to address certain issues and provide a more comprehensive approach. By using Laravel's built-in mime type validation or storing the uploaded file's mimetype as a variable, you can ensure that only valid video files are accepted for uploading onto your web application while maintaining a high level of security.