Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Video file validation

Is this how to validate a video file in laravel ?

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

because even if the file uploaded is a mov type, it will return that the file should be one of the types listed in the rule above.


How I ended up solving it:

As prompted from the answer's below I ended up storing the mime type for the uploaded file to a $mime variable like so:

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

Then had to write an if statement to check for video mime types:

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 } 
like image 249
cch Avatar asked Mar 13 '14 12:03

cch


People also ask

How to validate image and video in laravel?

Thankfully, Laravel allows us to create our own custom validation rules. We can create a custom rule closure that checks if the upload is an image or a video, then validates the rules based on the upload type. $request->validate([ 'file', // Confirm the upload is a file before checking its type.

How do I get the length of a video in laravel?

$video_file = $getID3->analyze('path-to-your-video'); // Get the duration in string, e.g.: 4:37 (minutes:seconds) $duration_string = $video_file['playtime_string']; // Get the duration in seconds, e.g.: 277 (seconds) $duration_seconds = $video_file['playtime_seconds'];


1 Answers

That code is checking for the extension but not the MIME type. You should use the appropriate MIME type:

 Video Type     Extension       MIME Type Flash           .flv            video/x-flv MPEG-4          .mp4            video/mp4 iPhone Index    .m3u8           application/x-mpegURL iPhone Segment  .ts             video/MP2T 3GP Mobile      .3gp            video/3gpp QuickTime       .mov            video/quicktime A/V Interleave  .avi            video/x-msvideo Windows Media   .wmv            video/x-ms-wmv 

If you are not sure the MIME type of the file you are testing you can try $file->getMimeType()

like image 173
marcanuy Avatar answered Oct 04 '22 02:10

marcanuy