2 votes

How to validate audio files (mp3, wav, etc) in Laravel 5.5?

I have a form where I have an input "File" to upload a file, the thing is that I want to upload ONLY audio files, I don't want to upload images or anything else, only audio. As I currently have it, it uploads any file to a path that I specify in config/filesystems.php

Here is my form:

<div class="form-control-file">
  <label for="file">Subir Cuña:</label>&nbsp; <a href="#" data-toggle="tooltip" data-placement="right" title="Se recomienda subir solo archivos de audio menores a 2 MB!"><i class="fas fa-info-circle"></i></a>
    <input type="file" name="file">
         <span style="color:red">{{$errors->first('archivo')}}</span>

My controller to save is as follows:

public function saveaudio($file){
    if(\Storage::disk('audios')->put($name_file, \File::get($file))){
        return $name_file;
        return false;


public function store(Request $request)

    $event = new Event();

    if (!empty($request->file)) {
         if ($name_file!=false){

        //Una forma de crear variables de session
        return back()->with(['msj'=>'Se realizó la operación con exito']);
        return redirect()->route('listspautas');
        // return redirect()->action('PautasController@index');
            return back()->with(['nomsj'=>'No se realizo la operación']);
            return redirect()->route('listspautas');

The question is: How to validate so that only audio files can be uploaded?

Thank you in advance.


configbug Points 368

The validation could be done from the frontend with Javascript, here is an example:

In your JS :

function validarExtensionArchivo(){
    var fileInput = document.getElementById('file');
    var filePath = fileInput.value;
    var allowedExtensions = /(\.wav|\.mp3|\.mp4|\.mid)$/i;
        alert('Solo se permite archivos de audio con esta extensión .wav/.mp3/.mp4/.mid .');
        fileInput.value = '';
        return false;
        //Otro Código

In your HTML :

<input type="file" id="file" onchange="return validarExtensionArchivo()" />

I share with you a Reference Link

0 votes

Thank you very much, I will try to share if it worked for me.

0 votes

It worked, thank you very much.


Aarón Gutiérrez Points 1422

In truth, server-side validation should not be missing, it's okay to validate client-side (and certainly not by launching a simple alert() since there is nothing UX launching those alerts) but server-side validation should not be missing, how about if I go into the code inspector and change the id to your input?

The server-side validation would look something like this:

//Hasta arriba de la clase importa Validator
use Validator;

$validator = Validator::make($request->all(), [
                'file' => 'required|file|mimes:mp3,mp4,wav,mid',

                return redirect()->back()->withErrors($validator);

If your input is optional just add a before: if($request->hasFile('file')){} to validate only when it exists in your request.

You can also create a Request by running the command php artisan make:request but your input should be required at all times, check the official documentation for more information:

For validation: https://laravel.com/docs/5.6/validation For requests: https://laravel.com/docs/5.6/validation#form-request-validation

I also add that it is a bad practice to put inline events in HTML tags because the code loses readability and to maintain it will cost you more work to be looking in HTML tags.

0 votes

Wow, thank you very much, I will try it that way to see.


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: