Search code examples
phplaravellaravel-5.3laravel-5.4laravel-validation

audio validation issues laravel 5.4


I am trying to validate my form request. What I'm doing is I am to upload audio, that is, mp3,wav etc, the issue I'm having is that it keeps throwing an error message back at me saying 'the file type must be mp3', I tried uploading an image and it said the file type must be mp3, I also tried uploading mp3 and it says the same thing, below is my audio controller.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;

class UploadController extends Controller
{
public function uploadsingle(Request $request)
{
    $validator = Validator::make($request->all(), [
        'song' => 'required|mimes:image/png',
    ]);

    if($validator->fails()){
        return redirect()->back()->withErrors($validator)->withInput();
    }

    ///save audio, etc
    echo 'validation passed';
}
}

this is my html code

<title>Upload page</title>
@include('layouts.page-life')
@include('layouts.navbar')




<h1>Upload Page</h1>
<br>
<form action="{{ route('doupload') }}" method="POST">
{{ csrf_field() }}
@if (count($errors) > 0)
        <div class="alert alert-danger alert-dismissible" role="alert">
            <ul>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif

<input name="song" type="file"/>
<br>
<button class="btn">Submit</button>
</form>

Solution

  • The following validation will accept wav, mp3 etc. This code is working for me.

    $validator = Validator::make($request->all(), [
      'song' => 'required|mimes:application/octet-stream,audio/mpeg,mpga,mp3,wav',
    ]);
    

    or write it in separate request file: eg: app/Http/Requests/SongStoreRequest.php

    public function rules(){
      ['song' => 'required|mimes:application/octet-stream,audio/mpeg,mpga,mp3,wav'];
    }