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.
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 }
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.
$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'];
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With